【问题标题】:ASP.NET, Entity Framework Code First many-to-many self referencing listsASP.NET,Entity Framework Code First 多对多自引用列表
【发布时间】:2021-02-08 04:49:38
【问题描述】:

我正在尝试在 ASP.NET 中使用 Code First 重新创建关注者/关注者系统。我找不到正确的方法来实现这个(MVC 模式)。 这是我的“用户”类:

public class User
{
    public User()
    {
        this.Followers = new HashSet<User>();
        this.Follows = new HashSet<User>();
    }
    
    [Key]
    public int UserId { get; set; }
    public string Mail { get; set; }
    public string Password { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Locality { get; set; }
    public string PhoneNumber { get; set; }
    public string Description { get; set; }
    public string ProfileImage { get; set; }

    public virtual ICollection<User> Followers { get; set; }
    public virtual ICollection<User> Follows { get; set; }
}

我已经能够使用名为“Subscriptions”的连接表创建数据库使用这个 dbcontext 类

public FakebookDBContext()
        : base("name=FakebookDBContext")
    {
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<User>()
                    .HasMany<User>(fer => fer.Follows)
                    .WithMany(fow => fow.Followers)
                    .Map(ff =>
                    {
                        ff.MapLeftKey("UserFollowerId");
                        ff.MapRightKey("UserFollowedId");
                        ff.ToTable("Subscription");
                    });

    }

在测试与我的“用户”类(自动生成)相关的 Api 时,我注意到两件事:

  1. 在使用关注现有用户的用户向我的 API 发送 POST 请求时,它会在数据库中创建两个新用户(订阅表已正确填写)

  2. 在向我的 API 发送 PUT 请求以尝试更改已存在的用户关注时,“订阅”表保持不变。

POST 示例:

输入:

{
"$id" : 1,
    "Followers": [
        {
            "$id": "2",
            "Followers": [],
            "Follows": [{"$ref": "1"}],
            "UserId": 1,
            "Mail": null,
            "Password": null,
            "LastName": null,
            "FirstName": null,
            "Locality": null,
            "PhoneNumber": null,
            "Description": null,
            "ProfileImage": null
        }
    ],
    "Follows": [],
    "Mail": null,
    "Password": null,
    "LastName": null,
    "FirstName": null,
    "Locality": null,
    "PhoneNumber": null,
    "Description": null,
    "ProfileImage": null
}

输出:

{
"$id": "1",
"UserId": 24,
"Mail": null,
"Password": null,
"LastName": null,
"FirstName": null,
"Locality": null,
"PhoneNumber": null,
"Description": null,
"ProfileImage": null,
"Followers": [
    {
        "$id": "2",
        "UserId": 25,
        "Mail": null,
        "Password": null,
        "LastName": null,
        "FirstName": null,
        "Locality": null,
        "PhoneNumber": null,
        "Description": null,
        "ProfileImage": null,
        "Followers": [],
        "Follows": [
            {
                "$ref": "1"
            }
        ]
    }
],
"Follows": []

}

我得出的结论是,我没有正确地将用户链接在一起。似乎没有找到对现有用户的引用,并且框架(?)只是创建了新对象。

我想避免创建“订阅”类并以这种方式保留用户模型(如果可能)。我是 IT 方面的新手,所以请随时就如何更好地实施这一点提出任何建议。谢谢!

【问题讨论】:

  • 这可能是因为 UserId 上缺少“ForeignKey”属性,但我真的不知道如何使它工作。
  • Many-Many 连接需要一个新表放在中间。
  • 感谢您的回答,但正如我的问题中所说,我设法创建了这个名为“订阅”的连接表(参见第二个代码 sn-p)
  • 您尚未从您的保存方法中发布任何代码。您需要确保重新加载或附加现有 User 实例及其 PK 值。

标签: c# asp.net-mvc ef-code-first many-to-many self-reference


【解决方案1】:

我认为最简单的方法是创建另外两个类。一种用于追随者,一种用于追随。每个班级将有一个User 和一个ICollection&lt;User&gt;

我知道你想避免创建新课程,但这是告诉 EF 你想做什么的最简单方法。

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 2011-04-06
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多