EF CF:与附加信息的多对多关系 [英] EF CF: many-to-many relation with additional info

查看:124
本文介绍了EF CF:与附加信息的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们拥有旧数据库,我们将新对象和道具映射到旧的表和列。到现在为止还挺好。我们有多对多关系成功映射。中间表包含附加数据。当我们尝试将中间表映射到一个对象时,我们得到了映射已被定义的异常。如果我们从关系的任何一边删除映射,我们得到错误,表中缺少(ofc,我们只期望)。我可以用NHibernate轻松地做到这一点,我开始认为EF缺少真正很多功能。所以,请告诉我我错了,我们可以用EF做这个。



最好的问候



编辑:这是一个失败的虚拟样本。

  class User 
{
public ICollection< User>关注者{get; set;}
}

class UserRelation
{
public User User {get; set;}
public User Follower {get; set;}
public DateTime CreatedOn {get; set;}
}






用户映射

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

用户关系映射

  modelBuilder 
.Entity< UserRelation>()
.ToTable(user_follower);

modelBuilder
.Entity< UserRelation>()
.HasOptional< User>(f => f.User)
.WithRequired() => m.MapKey(user_id));

modelBuilder
.Entity< UserRelation>()
.HasOptional< User>(f => f.Follower)
.WithRequired() = m.MapKey(follower_id));

modelBuilder
.Entity< UserRelation>()
.Property(entity => entity.CreatedOn)
.HasColumnName(created_on);

异常



指定的模式无效。错误:
(67,6):错误0019:具有模式'dbo'和表'user_follower'的EntitySet'UserUser'已经定义。每个EntitySet必须引用唯一的模式和表。



Edit2:这是另一个模型的例子: http://learnentityframework.com/LearnEntityFramework/tutorials/many-to-many-relationships-in-实体数据模型/

解决方案

直接多对多映射仅在连接表只包含外键。如果您想要在连接表中公开其他属性,则必须将其映射到单独的实体,并将两个一对多关系映射到多对多使用的前实体。



我实际上不能写你的代码,因为我不明白你的例子。



只尝试(不要映射多对多用户):

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

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


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/

解决方案

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();

这篇关于EF CF:与附加信息的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆