EF CF: many-to-many relation with additional info

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

We have legacy database and we map the new objects and props to the old tables and columns. So far so good. We have many-to-many relation which was mapped successfully. The intermediate table contains additional data. When we try to map the intermediate table to an object we get exception that the mapping is already defined. If we remove mapping from any side of the relation we get error that table is missing (ofc, we expect just that). I can do that easily with NHibernate and I am starting to think that EF is missing really really many features. So, please, tell me I am wrong and we can do that with EF.

Best regards

EDIT: here is a dummy sample which fails.

class User {   public ICollection<User> Followers{get;set;} }  class UserRelation {   public User User{get;set;}   public User Follower{get;set;}   public DateTime CreatedOn{get;set;} } 

user mapping

modelBuilder      .Entity<User>()      .HasMany<User>(user => user.Followers)      .WithMany()      .Map(m =>m.MapLeftKey("user_id").MapRightKey("follower_id")      .ToTable("user_follower")); 

user relation mapping

modelBuilder      .Entity<UserRelation>()      .ToTable("user_follower");  modelBuilder      .Entity<UserRelation>()      .HasOptional<User>(f => f.User)      .WithRequired().Map(m => m.MapKey("user_id"));  modelBuilder      .Entity<UserRelation>()      .HasOptional<User>(f => f.Follower)      .WithRequired().Map(m => m.MapKey("follower_id"));  modelBuilder      .Entity<UserRelation>()      .Property(entity => entity.CreatedOn)      .HasColumnName("created_on"); 

Exception

Schema specified is not valid. Errors: (67,6) : error 0019: The EntitySet 'UserUser' with schema 'dbo' and table 'user_follower' was already defined. Each EntitySet must refer to a unique schema and table.

Edit2: Here is another example of this model: http://learnentityframework.com/LearnEntityFramework/tutorials/many-to-many-relationships-in-the-entity-data-model/

回答1:

Direct many-to-many mapping is available only if junction table contains just foreign keys. If you want to expose other properties in junction table you must map it to separate entity and mapt two one-to-many relations from former entities used in many-to-many.

I'm actually not able to write you the code because I don't understand your example.

Try only (don't map Many-to-many in User):

modelBuilder.Entity<UserRelation>     .HasRequired(r => r.User)     .WithMany(u => u.Followers);  modelBuilder.Entity<UserRelation>     .HasRequired(r => r.Follower)     .WithMany(); 


回答2:

EF maps many-to-many relationships as properties of the related objects.

So, let's say you have Cars and Drivers that are related m-to-n. In your EF model, you will see that each Car object has a Drivers collection as a property, and each Driver object has a Cars collection as a property.

That is how m-to-n relationships are modeled in EF.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!