c# - Ef Code First 外键失败

标签 c# entity-framework ef-code-first

我有一个带有以下类的 codefirst 迁移的项目:

public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public virtual ICollection<Chat> Chats { get; set; }
    }

public class Chat

{
    [Key, Column(Order = 0)]
    public int User1Id { get; set; }

    public virtual User User1 { get; set; }

    [Key, Column(Order = 1)]
    public int User2Id { get; set; }

    public virtual User User2 { get; set; }

    public string Channel { get; set; }
}

思路是一个聊天有两个用户,聊天主键是两个用户Id的组合键,是外键。

用户还有一个他参与的聊天列表。

现在,当我尝试运行它时,出现以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.Chat_dbo.User_User2Id' on table 'Chat' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.

我有点明白发生了什么,但我不知道如何解决它。试过这个(来自这里某处的主题):

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Chat>()
                .HasRequired(s => s.User1)
                .WithMany()
                .WillCascadeOnDelete(true);

            modelBuilder.Entity<Chat>()
                .HasRequired(s => s.User2)
                .WithMany()
                .WillCascadeOnDelete(true);

            base.OnModelCreating(modelBuilder);
        }

但没有结果,我仍然得到错误。任何人都可以阐明这个问题吗?

最佳答案

尝试

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Chat> InitiatedChats { get; set; }
    public virtual ICollection<Chat> ParticipantInChats { get; set; }
}
public class Chat
{
    [Key, Column(Order = 0)]
    public int User1Id { get; set; }

    public virtual User User1 { get; set; }

    [Key, Column(Order = 1)]
    public int User2Id { get; set; }

    public virtual User User2 { get; set; }

    public string Channel { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Chat>()
        .HasRequired(s => s.User1)
        .WithMany(u => u.InitiatedChats)
        .HasForeignKey(s => s.User1Id)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Chat>()
        .HasRequired(s => s.User2)
        .WithMany(u => u.ParticipantInChats)
        .HasForeignKey(s => s.User2Id)
        .WillCascadeOnDelete(false);

    base.OnModelCreating(modelBuilder);
}

答案来自this发布

关于c# - Ef Code First 外键失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18228956/

相关文章:

c# - 在 EF Core 中按孙子属性过滤父集合

ef-code-first - 首先使用实体​​框架代码在两个实体之间创建 2 个关系

c# - Youtube API v3 .NET 集成

entity-framework - Azure 上的 MongoDB 辅助角色

c# - 现有的 DI 框架使用和 Silverlight

entity-framework-4 - TPH 继承映射使用 nchar (N'value') 而不是 char ('value' ) 作为鉴别器列值

entity-framework - Entity Framework Code First - 可以映射现有表并将其设置为只读吗?

c# - 导航时刷新 ViewModel 列表

c# - Word 到 PDF 转换后的 PDF 出现错误 - 拒绝访问错误

c# - 有没有一种简单的方法来计算递归数据结构中的元素?