开发者

EF CodeFirst: Mapping entities (one-many) to the same table

Lets say I have Customer, Order, OrderDetail classes in the busin开发者_StackOverflow社区ess layer (It's a simplified version of my problem).

I have also an old (existing) database that has one global table where every row of the table contains the information of Customers-Orders-OrderDetails; for example:

CustomerID, CompanyName,Fax,OrderID, OrderDate,ProductID,UnitPrice,Quantity

(in this way there are duplicated information of a Customer in different rows).

In the future I'll have a new database (with different table Customers, Orders, OrderDetails), and I want to use the same program.

I want to use EF CodeFirst to mapping to the old database and in the future to the new database

Which is the best solution?

  1. Design a business layer with a global class that contains the information of Customers-Orders-OrderDetails. So the mapping of this class with the old database using EF4 in the data layer is trivial. In the future I'll modify both business layer and data layer for the new database.

  2. Design a business layer with Customers, Orders, OrderDetails classes. In this case is it possible to map these classes to the global table of the old database? How ? (the problem is that the Customer-Order is one to many). In the future I'll modify only data layer for mapping the new database.


  1. This will work for now and later you will have to modify everything working with a global class - it can be a lot of work.
  2. It is not possible to map one table to three entities where two have one-to-many relation between them with EF.

Use third approach. Load one class as described in first approach but immediately convert result to three classes from the second approach. The reverse operation will be done in case of persisting changes. Wrap this code in single place - repository. Your application will use three classes and it will not have any knowledge about the way how they are persisted. Once you change the database you will only remove additional conversions from the repository and work directly with Customer, Order, OrderDetail loaded and persisted by EF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜