0

I'm using Entity Framework Core and have a simple 5 table design, but get an error and can't figure out why. What am I missing?

Exception:

Introducing FOREIGN KEY constraint 'FK_GraphicItems_GraphicUploadTemplateItems_GraphicUploadTemplateItemId' on table 'GraphicItems' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Base:

public abstract class Base
{
    public int Id { get; set; }
    public DateTime DateCreated { get; set; } = DateTime.Now;
}

Client:

public class Client : Base
{
    public Guid Key { get; set; }
    public string Name { get; set; }
}

Graphic:

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: (the property GraphicUploadTemplateId causes the exception)

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; }
}
SteinTheRuler
  • 3,549
  • 4
  • 35
  • 71
  • Possible duplicate of [Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?](https://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths) – Robyn MacCallum Jun 08 '17 at 15:30

2 Answers2

1

There will be a link to the corresponding GraphicUploadTemplateItem already through GraphicItem.UploadTemplateItem.Id, i think that causes the circular reference.

Kristóf Tóth
  • 791
  • 4
  • 19
0

Fixed the problem by setting the property type to nullable

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; }
}
SteinTheRuler
  • 3,549
  • 4
  • 35
  • 71