entity framework - No tables are created after setting WillCascadeOnDelete on true -
we making project in asp mvc4, , using code-first. means our database automaticly created our code after running it. noticed optional relationships in database dont have cascade on delete turned on default.
so after googling, found had add in context, gives me errors.
after set willcascadeondelete on true , delete database entity framework can create again, creates database no tables in. if put on false, works without problems.
here code use put on true:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<location>() .hasoptional(r => r.partoflocations) .withmany() .willcascadeondelete(); base.onmodelcreating(modelbuilder); }
here location model:
public class location { [key, databasegeneratedattribute(databasegeneratedoption.identity)] public int locationid { get; set; } [display(name = "name"), required] public string name { get; set; } [display(name = "description", resourcetype = typeof(viewres.classmodelstrings))] public string description { get; set; } public int locationtypeid { get; set; } [foreignkey("locationtypeid")] public virtual locationtype locationtype { get; set; } public virtual icollection<location> partoflocations { get; set; } public override string tostring() { return this.name; } }
also, if delete database quick after created, gives error: database xxx not accessible.
and adds " ( single user ) " after database name, no idea if helps
Comments
Post a Comment