更新时间:2023-11-20 21:51:28
快速演练:
的ObjectContext
继承揭露实体(通常在同一个项目中的.edmx文件)对象集< TEntity>
对于每次实体ObjectContext
to expose the entities (typically in the same project as the .edmx file)ObjectSet<TEntity>
for each of you entities样品code:
public class SalesContext : ObjectContext
{
public SalesContext(string connectionString, string defaultContainerName)
: base(connectionString, defaultContainerName)
{
this.Customers = CreateObjectSet<Customer>();
this.Products = CreateObjectSet<Product>();
this.Orders = CreateObjectSet<Order>();
this.OrderDetails = CreateObjectSet<OrderDetail>();
}
public ObjectSet<Customer> Customers { get; private set; }
public ObjectSet<Product> Products { get; private set; }
public ObjectSet<Order> Orders { get; private set; }
public ObjectSet<OrderDetail> OrderDetails { get; private set; }
}
这就是它......
That's about it...
重要提示:如果您使用自动代理创建的变化跟踪( ContextOptions.ProxyCreationEnabled
,默认情况下是如此),属性你的域类的必须是虚。这是必要的,因为通过EF 4.0生成的代理将覆盖它们来实现更改跟踪。
Important notice : if you use the automatic proxy creation for change tracking (ContextOptions.ProxyCreationEnabled
, which is true by default), the properties of your domain classes must be virtual. This is necessary because the proxies generated by EF 4.0 will override them to implement change tracking.
如果你不想使用自动代理的创建,您将需要处理的更改跟踪自己。请参见这个MSDN页细节
If you don't want to use automatic proxy creation, you will need to handle change tracking yourself. See this MSDN page for details