c# - EF Code First Migrations creating extra foreign key -
i'm trying create profile
table integrate asp.net identity table: aspnetusers
. i'm using code first migrations ef 6.0.
here user class:
public class applicationuser : identityuser { public applicationuser() { } public virtual string firstname { get; set; } public virtual string lastname { get; set; } public userstatus status { get; set; } public virtual profile profile { get; set; } }
here profile class:
public class profile { public int id { get; set; } public string applicationuserid { get; set; } public virtual applicationuser applicationuser { get; set; } }
i have configuration in datacontext:
modelbuilder.entity<profile>() .hasrequired<applicationuser>(m => m.applicationuser);
the weird thing when run migration creates applicationuserid , applicationuser_id in database when want create applicationuserid , use foreign key column. if add property appliationuser_id it'll add applicationuser_id1. know how fix this?
i want make sure foreign key on profile table i.e. profile has column applicationuserid , not vice versa i.e. don't want aspnetusers table have profile_id column.
p.s. i'm using fluent api, not data annotations.
if want configure 1 one relationship, entity framework requires primary key of dependent must used foreign key.
public class profile { public string applicationuserid { get; set; } public virtual applicationuser applicationuser { get; set; } }
now, fk convention, due restriction, since there no choice, code first infer fk you.
another thing is, proper way configure one-to-one relationship using fluent api is:
modelbuilder.entity<profile>() .hasrequired<applicationuser>(m => m.applicationuser) .withoptional(a=>a.profile);
Comments
Post a Comment