繁体   English   中英

迁移 - FOREIGN KEY 约束

[英]Migration - FOREIGN KEY constraint

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

在表 'Users' 上引入 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; }  
}

那是因为你搞砸了关系

用户 -> 城市、战役、房间

活动 -> 公司

房间 -> 活动,用户(创建者)

消息 -> 用户,房间

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

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

用户 -> 城市

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

活动 -> 公司

房间 -> 活动,用户(创建者)

消息 -> 用户,房间

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM