entity framework - WithRequiredPrincipal, why still have to define Required or ForeignKey to be able to compile? -
public class admin : entitytypeconfiguration<admin> { //[foreignkey("blog")] -- if enable this, compiles public int adminid { get; set; } public string adminname { get; set; } public string adminpicture { get; set; } //[required] -- or if enable this, compiles public virtual blog blog { get; set; } } public class blog : entitytypeconfiguration<blog> { public int blogid { get; set; } public string blogname { get; set; } public string blogurl { get; set; } public virtual admin admin { get; set; } public blog() { hasrequired(a => a.admin).withrequiredprincipal(b=>b.blog); } }
as long defining hasrequired , withrequiredprincipal keys, why vs still creates below error.
unable determine principal end of association between types 'dummy.models.blog' , 'dummy.models.admin'. principal end of association must explicitly configured using either relationship fluent api or data annotations.
second thing is, enable [required] or [foreingkey] attr., in edmx designer, see 1 - 0..1 must see 1 - 1 (both end required)
1-1 relationship not possible @ database level, because can't insert 2 rows @ same time. 1-1 possible @ class validation level.
to make 1-1 relationship, primary key of dependant entity must foreign key of principal entity; that's way make 1-1 relationship. so, have make following changes (considering using ef code first):
public class admin { public int adminid { get; set; } public string adminname { get; set; } public string adminpicture { get; set; } public virtual blog blog { get; set; } }
blog should not have own blogid, because blog belongs admin, , admin can have 1 blog (1-1 relationship). if create blogid, adminid fk, making 1-n relationship. furthermore, not mix entity class mapping class, should different things. see example below:
public class blog { public int adminid { get; set; } //pk , fk public string blogname { get; set; } public string blogurl { get; set; } public virtual admin admin { get; set; } }
creating relationship mapping class:
public class blogmapping : entitytypeconfiguration<blog> { public blogmapping() { haskey(i => i.adminid); hasrequired(a => a.admin) .withrequireddependent(i => i.blog); } }
register mapping inside dbcontext class:
public class mydbcontext : dbcontext { public dbset<admin> admins { get; set; } public dbset<blog> blogs { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.configurations.add(new blogmapping()); } }
this generate following migration:
createtable( "dbo.admins", c => new { adminid = c.int(nullable: false, identity: true), adminname = c.string(), adminpicture = c.string(), }) .primarykey(t => t.adminid); createtable( "dbo.blogs", c => new { adminid = c.int(nullable: false), blogname = c.string(), blogurl = c.string(), }) .primarykey(t => t.adminid) .foreignkey("dbo.admins", t => t.adminid) .index(t => t.adminid);
hope helps!
Comments
Post a Comment