【问题标题】:How to use fluentApi to manage nested relations?如何使用fluentApi管理嵌套关系?
【发布时间】:2014-04-19 21:54:47
【问题描述】:

我有一个用户模型

public class User {
    public Guid IdGuid {get;set;}
    public string Name {get;set;}
    public virtual List<User> Friends {get;set;}
}

我还有第二个模型

public class UserFriendship{
    public Guid FriendShipIdGuid {get;set;}
    public Guid UserIdGuid {get;set;}
    public Guid FriendIdGuid {get;set;}
}

我在上下文中有 FluentApi 代码 OnModelcreating

modelBuilder.Entity<User>().HasMany(u => u.Friends).WithMany().Map(c => {
    c.MapLeftKey("UserIdGuid"); 
    c.MapRightKey("FriendIdGuid");
    c.ToTable("UserFriendship");
    });

问题:我已经有一个UserFriendship 类模型。我想创建有关用户与此模型的关系。我正在使用 FluentApi 进行操作。

但在 FluentApi 代码执行后,FriendShipIdGuid 会从 UserFriendship 表中删除。

我需要为用户创建一对多关系,并将其保存到表UserFriendship 并将它已经存在的类模型名称映射到UserFriendship dbset 的哪个元素。 你能帮帮我吗?

作为总结,我已经有UserFriendship 类。而这个类包含FrientshipGuidId(Key)、UserIdGuidFriendIdGuid。 我需要将User 类一对多映射到自身。 我想将这种关系保存在UserFriendship 类上。 我也想使用Userfriendship 作为数据库集。

我有

public DbSet<UserFriendship> Friendships { get;set; }

在我的上下文中。

我正在使用 FluentApi ToTable 函数创建一对多关系表。 但是 FluentApi 代码删除了FriendshipIdGuid 字段。

【问题讨论】:

  • 您是否尝试将 Code First 与现有表一起使用?
  • @mostruash 是的。流利创建的现有表。但我也需要将此 UserFriendship 用作数据库集。我将把这个友谊数据用于用户类属性。该属性是 List Friends 。那可能吗 ?我需要从 UserFriendship 表中生成 User.Friends 列表,我还需要使用 UserFriendship 作为 DbSet。

标签: c# sql-server entity-framework map fluent


【解决方案1】:

这是不可能的。您不能在 User 类中使用 List&lt;UserFriendship&gt;,因为有时用户是用户,有时用户是朋友,除非您的 UserFriendship 表中有重复项,如下所示:

Friendship table:          |
----------------------------

User / Friend
-------------
  A  /   B
  B  /   A
-------------

首先将UserUserFriendship类改成如下:

class UserFriendship 
{
    public Guid FriendShipIdGuid {get;set;}
    public Guid UserIdGuid {get;set;}
    public Guid FriendIdGuid {get;set;}

    public User User {get;set;}
    public User Friend {get;set;}
}

class User 
{
    public Guid IdGuid {get;set;}
    public string Name {get;set;}

    // Only if you will allow duplicates in UserFriendship
    public virtual List<UserFriendship> Friendships {get;set;}
}

我不记得 Fluent API 的确切语法/方法,但你会明白的:

modelBuilder.Entity<User>()
  .HasKey(u => u.IdGuid)
  .ToTable("User");

modelBuilder.Entity<UserFriendship>()
  .HasKey(uf => uf.FriendShipIdGuid)
  .ToTable("UserFriendship");

modelBuilder.Entity<UserFriendship>()
  .HasRequired(uf => uf.User)
  .WithMany(u => u.Friendships)       // Only if you allow duplicates
  //.WithMany()                         // Otherwise
  .HasForeignKey(uf => uf.UserIdGuid);

modelBuilder.Entity<UserFriendShip>()
   .HasRequired(uf => uf.Friend)
   .WithMany()
   .HasForeignKey(uf => uf.FriendIdGuid);

【讨论】:

  • 你说的重复是什么意思?例如 1 与 2 的朋友和 2 与 1 的朋友。你是这个意思吗?另一个想法是,您评论了 //.WithMany() 。如果我不想使用重复项,我应该使用删除这个 .WithMany() 行吗?
  • 是的,我是这个意思。如果您不想使用重复项,请使用.WithMany()。从User 类中删除List&lt;UserFriendship&gt; Friendships。我建议你使用重复。它会让你的生活更轻松。如果这对您有帮助,请接受作为答案。
  • 我无法用 fluentApi 解决它 :( 尝试其他方法。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2021-10-13
相关资源
最近更新 更多