【问题标题】:Migration - FOREIGN KEY constraint迁移 - FOREIGN KEY 约束
【发布时间】:2023-03-22 11:24:01
【问题描述】:

我在运行迁移时使用 Code First,但出现错误。

在表“用户”上引入 FOREIGN KEY 约束“FK_dbo.Users_dbo.Campaigns_CampaignID”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束。查看以前的错误。

拜托,我需要帮助,我不明白为什么会发生这个错误。

我看到了其他类似的出版物,但我无法解决我的问题。

谢谢,

public class User  
{  
    public int ID { get; set; }  
    [StringLength(50, MinimumLength = 3)]  
    public string UserName { get; set; }  
    public string Password { get; set; }  
    [StringLength(50, MinimumLength = 3)]  

    public int CityID { get; set; }  
    public virtual City City { get; set; }  

    public int CampaignID { get; set; }  
    public virtual Campaign Campaign { get; set; }             

    public int RoomID { get; set; }  
    public virtual Room Room { get; set; }  

    public bool Status { get; set; }  
    public DateTime RegisterDate { get; set; }  
    public int UserCreator { get; set; }  
}

public class City  
{  
    public int ID { get; set; }  
    [StringLength(50, MinimumLength = 3)]  
    public string Name { get; set; }  

    public virtual ICollection Users { get; set; }  
}

public class Campaign  
{                
    public int ID { get; set; }  
    [StringLength(50, MinimumLength = 3)]  
    public string Name { get; set; }  

    public int CompanyID { get; set; }  
    public virtual Company Company { get; set; }  

    public virtual ICollection Users { get; set; }  
    public virtual ICollection Rooms { get; set; }  
}  

public class Company  
{  
    public int ID { get; set; }  
    [StringLength(30, MinimumLength = 3)]  
    public string Name { get; set; }  

    public virtual ICollection Campaigns { get; set; }  
}

public class Room  
{  
    public int ID { get; set; }  
    [MaxLength(200)]  
    public string RoomKey { get; set; }  
    [StringLength(30, MinimumLength = 3)]  
    public string Name { get; set; }         

    public int UserCreator { get; set; }  
    public virtual User User { get; set; }  

    public int CampaignID { get; set; }  
    public virtual Campaign Campaign { get; set; }  

    public virtual ICollection Messages { get; set; }  
}  

public class Message  
{  
       public int ID { get; set; }  
       public string Content { get; set; }          
       public string Timestamp { get; set; }  

       public int UserID { get; set; }  
       public virtual User User { get; set; }  

       public int RoomID { get; set; }  
       public virtual Room Room { get; set; }  
}

【问题讨论】:

    标签: c# sql asp.net


    【解决方案1】:

    那是因为你搞砸了关系

    用户 -> 城市、活动、房间

    活动 -> 公司

    房间 -> 广告系列、用户(创建者)

    消息 -> 用户,房间

    我猜您将 CampaignID 和 RoomID 设置为 User 的原因是您想知道谁加入了哪个广告系列以及哪个房间。但这是错误的。

    您需要的是用户的主数据,您只需要用户信息,与活动或房间无关。这些关系被视为交易数据,这意味着今天用户A可以加入这个房间,但明天用户A可以加入另一个房间。全年,1 位用户可以加入许多房间、许多活动。我建议的解决方案是

    用户 -> 城市

    RoomAttendees -> UserID, RoomID(这里不需要 CampaignID,因为 Room 已经获得了对 Campaign 的引用)

    活动 -> 公司

    房间 -> 广告系列、用户(创建者)

    消息 -> 用户,房间

    【讨论】:

    • 感谢您的回复,对我帮助很大。我做了一些改变,它奏效了。
    • 很高兴它有帮助。如果您不介意,请接受答案。
    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2019-10-05
    相关资源
    最近更新 更多