2016-06-21 99 views
0

我想从我的模型创建我的数据库,但我不断收到错误Introducing FOREIGN KEY constraint 'FK_dbo.Reports_dbo.UserProfiles_UserId' on table 'Reports' 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.EF 6个周期或多个级联路径

有谁知道什么可能是错误与我的模型/设置?

这些都是用模型

public class Report { 
    [Key] 
    public int Id { get; set; } 

    [Required] 
    public string Number { get; set; } 
    public bool Synced { get; set; } 

    public DateTime CreationDate { get; set; } 

    public int NewCommentId { get; set; } 
    public virtual Comment NewComment { get; set; } 

    public int UserId { get; set; } 
    public virtual UserProfile User { get; set; } 

    public virtual ICollection<Comment> Comments { get; set; } 
    public virtual ICollection<Photo> PhotosBefore { get; set; } 
    public virtual ICollection<Photo> PhotosAfter { get; set; } 
} 
public class Photo { 
    [Key] 
    public int Id { get; set; } 
    public string Image { get; set; } 
    public bool Synced { get; set; } 

    public DateTime CreationDate { get; set; } 

    public int ReportId { get; set; } 
    public virtual Report Report { get; set; } 

    public int UserId { get; set; } 
    public virtual UserProfile User { get; set; } 
} 
public class Comment { 
    [Key] 
    public int Id { get; set; } 

    public DateTime CreationDate { get; set; } 
    public string Text { get; set; } 

    public int ReportId { get; set; } 
    public virtual Report Report { get; set; } 

    public int UserId { get; set; } 
    public virtual UserProfile User { get; set; } 
} 
public class UserProfile { 
    [Key] 
    public int Id { get; set; } 
    public string Stamnummer { get; set; } 
    public string Leverancier { get; set; } 


    public virtual ICollection<Comment> Comments { get; set; } 
    public virtual ICollection<Report> Reports { get; set; } 
    public virtual ICollection<Photo> Photos { get; set; } 
} 

回答

0

为了一定的,我们需要看到你是如何使用的OnModelCreating方法模型构建器配置你的人际关系。根据您提供的错误消息,您似乎已配置关系,以便您的一个实体配置为从两个或更多其他实体级联删除。

作为一个例子(这可能不是这种情况,而是仅仅描述该问题的手段):

User具有Comments

User一对多的关系具有一维对许多与Reports

Report关系与Comments

一个一对多的关系被配置为使得一个User需要

Comment被配置为使得一个Report需要

Report被配置为使得一个User需要

任何一个一对多关系,其中该实体one关系的一方是要求级联删除配置默认情况下。在这种情况下,如果User被删除,它会触发级联到ReportsComments。每个Report也会导致Comments级联。

解决方法是禁用一个关系的级联删除。你可以在你的here找到类似的问题,描述我上面提到的。

相关问题