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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -