I have two classes, one of them holds nullable ID property of other (as foreign key) and virtual navigation property. When I delete my entity, I want it's ID to be deleted from other tables where hold as foreign key. It doesn't happen and I get error:
"The DELETE statement conflicted with the REFERENCE constraint FK_MyDb.ArtWorks_MyDb.ImageFiles_ImgId. The conflict occurred in database MyDatabase , table MyDb.ArtWorks, column 'ImgId'. The statement has been terminated."
Here is the entity holding foreign key:
public class ArtWork : EntityBase
{
public int ID { get; set; }
public int? ImgId { get; set; }
public virtual ImageFile Img { get; set; }
}
And this is the entity which when I delete error occurs.
public class ImageFile
{
public int ID { get; set; }
}
As far as I can see on migration file things are properly created:
CreateTable(
"MyDb.ArtWorks",
c => new
{
ID = c.Int(nullable: false, identity: true),
ImgId = c.Int()
})
.PrimaryKey(t => t.ID)
.ForeignKey("MyDb.ImageFiles", t => t.ImgId)
.Index(t => t.ImgId);
Other one:
CreateTable(
"MyDb.ImageFiles",
c => new
{
ID = c.Int(nullable: false, identity: true)
})
.PrimaryKey(t => t.ID);
I've deleted here irrelevant properties to be brief.
I've tried out few fluent api definitions, none changed the situation. What is wrong with these definitions?
Thank you.