I have this model:
public class ContentType
{
public int ContentTypeId{get;set;}
public string Name{get;set;}
public Lang Lang{get;set;}
public bool IsPublished{get;set;}
public int? ParentId { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ContentType Parent { get; set; }
public virtual List<ContentType> Children { get; set; }
}
It has a one to many relation to itself.
And in Context I have this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ContentType>().HasMany(c => c.Children).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
But when I am saving a record with ParentId=0
I see this error:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Note that the relation in database is not exist because of the following error:
'ContentType' table - Unable to create relationship 'FK_ContentType_ContentType'. The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_ContentType_ContentType". The conflict occurred in database "CMS", table "dbo.ContentType", column 'ContentTypeId'.
But I don't think the problem be from here.I don't know.
What is it wrong here?