我正在使用Entity Framework Core并且有一个简单的5表设计,但是得到一个错误并且无法找出原因。我错过了什么?
例外:
引入FOREIGN KEY约束 'FK_GraphicItems_GraphicUploadTemplateItems_GraphicUploadTemplateItemId' 在表'GraphicItems'上可能会导致循环或多个级联路径。 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他 FOREIGN KEY约束。
基
public abstract class Base
{
public int Id { get; set; }
public DateTime DateCreated { get; set; } = DateTime.Now;
}
客户端:
public class Client : Base
{
public Guid Key { get; set; }
public string Name { get; set; }
}
图形:
public class Graphic : Base
{
public int ClientId { get; set; }
public virtual Client Client { get; set; }
public ICollection<GraphicItem> Items { get; set; } = new HashSet<GraphicItem>();
public ICollection<Tag> Tags { get; set; } = new HashSet<Tag>();
}
GraphicItem: (属性GraphicUploadTemplateId导致异常)
public class GraphicItem : Base
{
public int GraphicId { get; set; }
public virtual Graphic Graphic { get; set; }
public int GraphicUploadTemplateItemId { get; set; }
public virtual GraphicUploadTemplateItem UploadTemplateItem { get; set; }
}
GraphicUploadTemplate:
public class GraphicUploadTemplate : Base
{
public int ClientId { get; set; }
public virtual Client client { get; set; }
public ICollection<GraphicUploadTemplateItem> Items { get; set; }
}
GraphicUploadTemplateItem:
public class GraphicUploadTemplateItem : Base
{
public int GraphicUploadTemplateId { get; set; }
public virtual GraphicUploadTemplate UploadTemplate { get; set; }
}
答案 0 :(得分:1)
将通过GraphicItem.UploadTemplateItem.Id链接到相应的GraphicUploadTemplateItem,我认为这会导致循环引用。
答案 1 :(得分:0)
通过将属性类型设置为可为空
来解决此问题public class GraphicItem : Base
{
public int GraphicId { get; set; }
public virtual Graphic Graphic { get; set; }
public int? GraphicUploadTemplateItemId { get; set; }
public virtual GraphicUploadTemplateItem UploadTemplate { get; set; }
}