如何:将对象服务用于自定义对象(实体框架)
即使使用自定义数据类,仍然可以利用由 Entity Framework 工具所生成的数据类提供的对象服务功能。这包括下列功能:
实例化特定于 Entity Data Model (EDM) 的 ObjectContext 的功能,包括预定义的连接。
返回特定于类型的 ObjectQuery 对象的属性。
将对象添加到特定实体集的自定义方法。
若要充分利用对象服务功能,最简单的方法是将从 ObjectContext 继承的所生成的类添加到项目。此类由 Entity Framework 工具基于您的 EDM 生成。
在 C# 项目中使用生成的对象代码
从命令提示符下,导航至模型的概念性架构定义语言 (CSDL) 文件所在的位置,然后运行以下命令(删除换行符):
%windir%\Microsoft.NET\Framework\v3.5\edmgen.exe /mode:EntityClassGeneration /incsdl:.\YourModel.csdl /outobjectlayer:.\YourModel.context.cs /language:CSharp
此时将基于指定的 CSDL 文件在 C# 中重新生成对象层。
打开新生成的代码文件,复制自 ObjectContext 继承的类,并将此类复制到自定义数据类代码文件中。
注意 还可以删除数据类并将剩下的类添加到项目中。
在 Visual Basic 项目中使用生成的对象代码
从命令提示符下,导航至模型的 CSDL 文件所在的位置,然后运行以下命令(删除换行符):
%windir%\Microsoft.NET\Framework\v3.5\edmgen.exe /mode:EntityClassGeneration /incsdl:.\YourModel.csdl /outobjectlayer:.\YourModel.context.vb /language:VB
此时将基于指定的 CSDL 文件在 Visual Basic 中重新生成对象层。
打开新生成的代码文件,复制自 ObjectContext 继承的类,并将此类复制到自定义数据类代码文件中。
注意 还可以删除数据类并将剩下的类添加到项目中。
示例
本示例说明所生成的支持 Order 和 LineItem 自定义数据类的对象上下文代码。
Option Strict Off
Option Explicit On
<Assembly: Global.System.Data.Objects.DataClasses.EdmSchemaAttribute("9f3bb474-6454-4ff1-911e-4a8be227e97c")>
Namespace Microsoft.Samples.Edm
'''<summary>
'''There are no comments for SalesOrdersEntities in the schema.
'''</summary>
Partial Public Class SalesOrdersEntities
Inherits Global.System.Data.Objects.ObjectContext
'''<summary>
'''Initializes a new SalesOrdersEntities object using the connection string found in the 'SalesOrdersEntities' section of the application configuration file.
'''</summary>
Public Sub New()
MyBase.New("name=SalesOrdersEntities", "SalesOrdersEntities")
End Sub
'''<summary>
'''Initialize a new SalesOrdersEntities object.
'''</summary>
Public Sub New(ByVal connectionString As String)
MyBase.New(connectionString, "SalesOrdersEntities")
End Sub
'''<summary>
'''Initialize a new SalesOrdersEntities object.
'''</summary>
Public Sub New(ByVal connection As Global.System.Data.EntityClient.EntityConnection)
MyBase.New(connection, "SalesOrdersEntities")
End Sub
'''<summary>
'''There are no comments for LineItem in the schema.
'''</summary>
<Global.System.ComponentModel.BrowsableAttribute(False)> _
Public ReadOnly Property LineItem() As Global.System.Data.Objects.ObjectQuery(Of LineItem)
Get
If (Me._LineItem Is Nothing) Then
Me._LineItem = MyBase.CreateQuery(Of LineItem)("[LineItem]")
End If
Return Me._LineItem
End Get
End Property
Private _LineItem As Global.System.Data.Objects.ObjectQuery(Of LineItem) = Nothing
'''<summary>
'''There are no comments for Order in the schema.
'''</summary>
<Global.System.ComponentModel.BrowsableAttribute(False)> _
Public ReadOnly Property Order() As Global.System.Data.Objects.ObjectQuery(Of Order)
Get
If (Me._Order Is Nothing) Then
Me._Order = MyBase.CreateQuery(Of Order)("[Order]")
End If
Return Me._Order
End Get
End Property
Private _Order As Global.System.Data.Objects.ObjectQuery(Of Order) = Nothing
'''<summary>
'''There are no comments for LineItem in the schema.
'''</summary>
Public Sub AddToLineItem(ByVal lineItem As LineItem)
MyBase.AddObject("LineItem", lineItem)
End Sub
'''<summary>
'''There are no comments for Order in the schema.
'''</summary>
Public Sub AddToOrder(ByVal order As Order)
MyBase.AddObject("Order", order)
End Sub
End Class
End Namespace
[assembly: System.Data.Objects.DataClasses.EdmSchemaAttribute()]
namespace Microsoft.Samples.Edm
{
/// <summary>
/// There are no comments for SalesOrdersEntities in the schema.
/// </summary>
public partial class SalesOrdersEntities : global::System.Data.Objects.ObjectContext
{
/// <summary>
/// Initializes a new SalesOrdersEntities object using the connection string found in the 'SalesOrdersEntities' section of the application configuration file.
/// </summary>
public SalesOrdersEntities() :
base("name=SalesOrdersEntities", "SalesOrdersEntities")
{
}
/// <summary>
/// Initialize a new SalesOrdersEntities object.
/// </summary>
public SalesOrdersEntities(string connectionString) :
base(connectionString, "SalesOrdersEntities")
{
}
/// <summary>
/// Initialize a new SalesOrdersEntities object.
/// </summary>
public SalesOrdersEntities(global::System.Data.EntityClient.EntityConnection connection) :
base(connection, "SalesOrdersEntities")
{
}
/// <summary>
/// There are no comments for LineItem in the schema.
/// </summary>
[global::System.ComponentModel.BrowsableAttribute(false)]
public global::System.Data.Objects.ObjectQuery<LineItem> LineItem
{
get
{
if ((this._LineItem == null))
{
this._LineItem = base.CreateQuery<LineItem>("[LineItem]");
}
return this._LineItem;
}
}
private global::System.Data.Objects.ObjectQuery<LineItem> _LineItem = null;
/// <summary>
/// There are no comments for Order in the schema.
/// </summary>
[global::System.ComponentModel.BrowsableAttribute(false)]
public global::System.Data.Objects.ObjectQuery<Order> Order
{
get
{
if ((this._Order == null))
{
this._Order = base.CreateQuery<Order>("[Order]");
}
return this._Order;
}
}
private global::System.Data.Objects.ObjectQuery<Order> _Order = null;
/// <summary>
/// There are no comments for LineItem in the schema.
/// </summary>
public void AddToLineItem(LineItem lineItem)
{
base.AddObject("LineItem", lineItem);
}
/// <summary>
/// There are no comments for Order in the schema.
/// </summary>
public void AddToOrder(Order order)
{
base.AddObject("Order", order);
}
}
}
另请参见
任务
如何:从 EntityObject 和 ComplexObject 基类继承(实体框架)
如何:实现自定义数据类接口(实体框架)