asp.net mvc - Code First Foreign Key Configuration in Many to Many Relationships -
i have created 2 table.
table 1: parent table parentid , parentname fields.in parentid primary key.
table 2: child table childid , childname fields.in childid primary key.
want parentid , childid table.so icreated table in name mappingparentchild parentid1,childid1.i want parentid1 , childid1 foreign key. how can achive this.
public class parent { public int parentid { get; set; } //primary key public string parentname { get; set; } } public class child { public int childid { get; set; } //primary key public string childname { get; set; } } public class mappingparentchild { public int id { get; set; } public int parentid1 { get; set; } // want foreign key parent(parentid) public int childid1 { get; set; }// want foreign key child(childid) } public class mappingparentchildconfiguration :entitytypeconfiguration<mappingparentchild> { public mappingparentchildconfiguration() { totable("mappingparentchild"); property(m => m.id).isrequired(); property(m => m.parentid1).isrequired(); property(m => m.childid1).isrequired(); } } how can this.please me.
public class parent { public int parentid { get; set; } public string parentname { get; set; } public virtual icollection<mappingparentchild> parent{ get; set; } } public class child { public int childid { get; set; } public string childname { get; set; } public virtual icollection<mappingparentchild> child { get; set; } } public class mappingparentchild { public int id { get; set; } public int parentid1 { get; set; } public int childid1 { get; set; } public virtual parent pt{ get; set; } public virtual child ch{ get; set; } } public class mappingparentchildconfiguration : entitytypeconfiguration<mappingparentchild> { public mappingparentchildconfiguration() { totable("mappingparentchild"); property(m => m.id).isrequired(); hasrequired(m => m.pt).withmany(e => e.parent).hasforeignkey(m => m.parentid ); hasrequired(m => m.ch).withmany(e => e.child ).hasforeignkey(m => m.childid ); } }
Comments
Post a Comment