Quick walkthrough:
- Create an entity data model (.edmx) in Visual Studio and clear the "custom tool" property of the edmx file to prevent code generation
- Create entities in your entity data model with the same names as your domain classes. Object properties must also have the same names and types as in domain classes.
- Create a class inherited from
ObjectContext to open objects (usually in the same project as the .edmx file) - In this class, create a property of type
ObjectSet<TEntity> for each of your entities
Code example:
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; } }
What about this ...
Important note: if you use automatic proxy creation to track changes ( ContextOptions.ProxyCreationEnabled , which is true by default), the properties of your domain classes should be virtual >. This is necessary because proxies created by EF 4.0 override them to implement change tracking.
If you do not want to use automatic proxy creation, you will need to track the changes yourself. See this MSDN page for more details.
source share