开发者

How Does Entity Framework Model Data from a Connecting Table?

Let's say I have a many-to-many relationship between the tables Customers and Trucks. They are joined by the table CustomersTrucks.

Now I'd like to add a column to Customers. However, the column is specific to customers associated with trucks, and not custo开发者_如何学JAVAmers that are associated with other tables, such as Vans.

I might therefore conclude that it makes better sense to add this new column to CustomerTrucks instead of Customers. That way, I'm not adding data to the Customers table that would be unused in some cases.

But if I add the new column to the connecting table, what would that do to the entity model? Normally, I would expect the Customers entity to have a collection property called Trucks. So how would my new column in the connecting table be modeled?


If you have normal junction table with only two columns (FKs for interconnected tables) you will not use such table as entity because EF can hide it and handle many-to-many relation directly.

Once you add any additional column to junction table it is not junction table any more and form EF perspective it is not many-to-many table any more (I'm not sure if it is still many-to-many in database terminology). Your CustomersTrucs became full entity with its own data which must be set in the application.

As you described your new property is not part of Customers table and because of that it also doesn't belong to Customer entity - you need CustomersTruck entity in your model and this entity will have two one-to-many relations (one to Customer and one to Truck). Because of that Customer as well as Truck will now have navigation property CustomersTrucks.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜