c# - Entity Framewrok Code First: Can not rename FK in optional 1:1 relationship -
consider following parent/child model:
public class parent { public int id { get; set; } public virtual child mychild { get; set; } } public class child { public int id { get; set; } public int? pid { get; set; } [foreignkey("pid")] public virtual parent myparent { get; set; } }
and context:
class context : dbcontext { public dbset<one_to_one.case1.child> child { get; set; } public dbset<one_to_one.case1.parent> parent { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<parent>() .hasoptional(p => p.mychild) .withoptionalprincipal(p => p.myparent); } }
it generates table schema:
create table [dbo].[children] ( [id] int identity (1, 1) not null, [myparent_id] int null, constraint [pk_dbo.children] primary key clustered ([id] asc), constraint [fk_dbo.children_dbo.parents_myparent_id] foreign key ([myparent_id]) references [dbo].[parents] ([id]) ); create table [dbo].[parents] ([id] int identity (1, 1) not null);
while database generated expected schema, surprised why foreign key in children table not pid
. help?
Comments
Post a Comment