LinqToEntitiesDomainService<TContext> Class

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

Provides a base class for domain services operating on LINQ to Entities data models.

Inheritance Hierarchy

System.Object
System.ServiceModel.DomainServices.Server.DomainService
System.ServiceModel.DomainServices.EntityFramework.LinqToEntitiesDomainService<TContext>

Namespace: System.ServiceModel.DomainServices.EntityFramework
Assembly: System.ServiceModel.DomainServices.EntityFramework (in System.ServiceModel.DomainServices.EntityFramework.dll)

Syntax

'Declaration
<LinqToEntitiesDomainServiceDescriptionProviderAttribute> _
Public MustInherit Class LinqToEntitiesDomainService(Of TContext As {New, ObjectContext}) _
    Inherits DomainService
'Usage
Dim instance As LinqToEntitiesDomainService(Of TContext)
[LinqToEntitiesDomainServiceDescriptionProviderAttribute]
public abstract class LinqToEntitiesDomainService<TContext> : DomainService
where TContext : new(), ObjectContext
[LinqToEntitiesDomainServiceDescriptionProviderAttribute]
generic<typename TContext>
where TContext : gcnew(), ObjectContext
public ref class LinqToEntitiesDomainService abstract : public DomainService
[<AbstractClassAttribute>]
[<LinqToEntitiesDomainServiceDescriptionProviderAttribute>]
type LinqToEntitiesDomainService<'TContext when 'TContext : new() and ObjectContext> =  
    class
        inherit DomainService
    end
JScript does not support generic types and methods.

Type Parameters

  • TContext
    The type of the LINQ to Entities ObjectContext.

The LinqToEntitiesDomainService<TContext> type exposes the following members.

Constructors

Name Description
Protected method LinqToEntitiesDomainService<TContext> Initializes a new instance of the LinqToEntitiesDomainService<TContext> class.

Top

Properties

Name Description
Protected property AuthorizationContext Gets or sets the optional template AuthorizationContext to use for IsAuthorized. (Inherited from DomainService.)
Protected property ChangeSet Gets the current ChangeSet. (Inherited from DomainService.)
Protected property ObjectContext Gets the ObjectContext for this LinqToEntitiesDomainService<TContext>.
Protected property ServiceContext Gets the active DomainServiceContext for this DomainService. (Inherited from DomainService.)
Protected property ServiceDescription Gets the DomainServiceDescription for this DomainService. (Inherited from DomainService.)
Protected property ValidationContext Gets or sets the optional ValidationContext to use for all validation operations invoked by the DomainService. (Inherited from DomainService.)

Top

Methods

Name Description
Protected method AuthorizeChangeSet Returns a value that indicates the whether the user is authorized to submit the specified ChangeSet. (Inherited from DomainService.)
Protected method Count<T>(IQueryable<T>) Returns the number of rows in the specified IQueryable object. (Overrides DomainService.Count<T>(IQueryable<T>).)
Protected method Count<T>(IQueryable<T>) Returns the number of rows in an IQueryable. (Inherited from DomainService.)
Protected method CreateObjectContext Creates and returns the ObjectContext instance that will be used by this provider.
Public method Dispose() Releases all resources used by the current instance of the DomainService class. (Inherited from DomainService.)
Protected method Dispose(Boolean) Releases all resources used by the LinqToEntitiesDomainService<TContext>. (Overrides DomainService.Dispose(Boolean).)
Public method Equals (Inherited from Object.)
Protected method ExecuteChangeSet Invokes the DomainOperationEntry for each operation in the ChangeSet. (Inherited from DomainService.)
Protected method Finalize (Inherited from Object.)
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method Initialize Initializes the domain service. (Overrides DomainService.Initialize(DomainServiceContext).)
Public method Invoke Invokes the specified operation. (Inherited from DomainService.)
Public method IsAuthorized Requests authorization for the specified DomainOperationEntry. (Inherited from DomainService.)
Protected method MemberwiseClone (Inherited from Object.)
Protected method OnError Called whenever an unrecoverable error occurs during the processing of a DomainService operation. (Inherited from DomainService.)
Protected method PersistChangeSet Finalizes changes after all the operations in the ChangeSet have been invoked. (Overrides DomainService.PersistChangeSet().)
Public method Query Performs the query operation indicated by the specified QueryDescription. (Inherited from DomainService.)
Protected method ResolveConflicts Resolves the conflicts when the submit fails due to optimistic concurrency conflicts.
Public method Submit Performs the operations indicated by the specified ChangeSet by invoking each of the corresponding domain operations. (Inherited from DomainService.)
Public method ToString (Inherited from Object.)
Protected method ValidateChangeSet Validates the whole ChangeSet before calling ExecuteChangeSet. (Inherited from DomainService.)

Top

Remarks

You create domain service classes that derive from the LinqToEntitiesDomainService<TContext> class when you expose LINQ to Entities types. When you use the Add New Domain Service Class dialog box to create a domain service that exposes LINQ to Entities types, the dialog box automatically creates a class that derives from LinqToEntitiesDomainService<TContext>. The class is created with methods that provide a starting point for implementing business logic in your application. A query method is included in the class. If you select the Enable Edit check box, insert, update, and delete methods are included. You should add methods or customize the existing methods to meet the requirement of your application.

You use the ObjectContext property to access the object that facilitates interacting with entity data objects.

Examples

The following example shows a domain service that derives from LinqToEntitiesDomainService<TContext>. It contains methods for querying, updating, and deleting data.

<EnableClientAccess()>  _
Public Class OrderDomainService
    Inherits LinqToEntitiesDomainService(Of AdventureWorksLT_DataEntities)

    Public Function GetSalesOrders() As IQueryable(Of SalesOrderHeader)
        Return Me.ObjectContext.SalesOrderHeaders.Include("SalesOrderDetails")
    End Function

    Public Sub UpdateSalesOrder(ByVal currentSalesOrderHeader As SalesOrderHeader)
        Dim originalOrder As SalesOrderHeader = Me.ChangeSet.GetOriginal(currentSalesOrderHeader)

        If (currentSalesOrderHeader.EntityState = EntityState.Detached) Then
            If (IsNothing(originalOrder)) Then
                Me.ObjectContext.Attach(currentSalesOrderHeader)
            Else
                Me.ObjectContext.AttachAsModified(currentSalesOrderHeader, Me.ChangeSet.GetOriginal(currentSalesOrderHeader))
            End If
        End If

        For Each detail As SalesOrderDetail In Me.ChangeSet.GetAssociatedChanges(currentSalesOrderHeader, Function(o) o.SalesOrderDetails)
            Dim op As ChangeOperation = Me.ChangeSet.GetChangeOperation(detail)

            Select Case op
                Case ChangeOperation.Insert
                    If ((detail.EntityState = EntityState.Added) _
                    = False) Then
                        If ((detail.EntityState = EntityState.Detached) _
                                    = False) Then
                            Me.ObjectContext.ObjectStateManager.ChangeObjectState(detail, EntityState.Added)
                        Else
                            Me.ObjectContext.AddToSalesOrderDetails(detail)
                        End If
                    End If
                Case ChangeOperation.Update
                    Me.ObjectContext.AttachAsModified(detail, Me.ChangeSet.GetOriginal(detail))
                Case ChangeOperation.Delete
                    If (detail.EntityState = EntityState.Detached) Then
                        Me.ObjectContext.Attach(detail)
                    End If
                    Me.ObjectContext.DeleteObject(detail)
            End Select
        Next
    End Sub

    Public Sub DeleteSalesOrder(ByVal salesOrderHeader As SalesOrderHeader)
        If (salesOrderHeader.EntityState = EntityState.Detached) Then
            Me.ObjectContext.Attach(salesOrderHeader)
        End If

        Select Case salesOrderHeader.Status
            Case 1 ' in process
                Me.ObjectContext.DeleteObject(salesOrderHeader)
            Case 2, 3, 4 ' approved, backordered, rejected
                salesOrderHeader.Status = 6
            Case 5 ' shipped
                Throw New ValidationException("The order has been shipped and cannot be deleted.")
        End Select

    End Sub
End Class
[EnableClientAccess()]
public class OrderDomainService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
{
    public IQueryable<SalesOrderHeader> GetSalesOrders()
    {
        return this.ObjectContext.SalesOrderHeaders.Include("SalesOrderDetails");
    }

    public void UpdateSalesOrder(SalesOrderHeader currentSalesOrderHeader)
    {
        SalesOrderHeader originalOrder = this.ChangeSet.GetOriginal(currentSalesOrderHeader);

        if ((currentSalesOrderHeader.EntityState == EntityState.Detached))
        {
            if (originalOrder != null)
            {
                this.ObjectContext.AttachAsModified(currentSalesOrderHeader, this.ChangeSet.GetOriginal(currentSalesOrderHeader));
            }
            else
            {
                this.ObjectContext.Attach(currentSalesOrderHeader);
            }
        }

        foreach (SalesOrderDetail detail in this.ChangeSet.GetAssociatedChanges(currentSalesOrderHeader, o => o.SalesOrderDetails))
        {
            ChangeOperation op = this.ChangeSet.GetChangeOperation(detail);
            switch (op)
            {
                case ChangeOperation.Insert:
                    if ((detail.EntityState != EntityState.Added))
                    {
                        if ((detail.EntityState != EntityState.Detached))
                        {
                            this.ObjectContext.ObjectStateManager.ChangeObjectState(detail, EntityState.Added);
                        }
                        else
                        {
                            this.ObjectContext.AddToSalesOrderDetails(detail);
                        }
                    }
                    break;
                case ChangeOperation.Update:
                    this.ObjectContext.AttachAsModified(detail, this.ChangeSet.GetOriginal(detail));
                    break;
                case ChangeOperation.Delete:
                    if (detail.EntityState == EntityState.Detached)
                    {
                        this.ObjectContext.Attach(detail);
                    }
                    this.ObjectContext.DeleteObject(detail);
                    break;
                case ChangeOperation.None:
                    break;
                default:
                    break;
            }
        }
    }

    public void DeleteSalesOrder(SalesOrderHeader salesOrderHeader)
    {
        if ((salesOrderHeader.EntityState == EntityState.Detached))
        {
            this.ObjectContext.Attach(salesOrderHeader);
        }

        switch (salesOrderHeader.Status)
        {
            case 1: // in process
                this.ObjectContext.DeleteObject(salesOrderHeader);
                break;
            case 2: // approved
            case 3: // backordered
            case 4: // rejected
                salesOrderHeader.Status = 6;
                break;
            case 5: // shipped
                throw new ValidationException("The order has been shipped and cannot be deleted.");
            default:
                break;
        }

    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.ServiceModel.DomainServices.EntityFramework Namespace