entity framework - Database First: how should I model Parent references Child references Parent? -


apologies if subject doesn't quite make sense here's issue:

in sql server have:

create table employee ( id int identity(1,1) , name varchar(50) not null );  create table notes ( id int identity(1,1) ,employee_id int not null , addedbyemployee_id int not null , text varchar(2000) );  alter table notes add constraint pk_notes primary key (id);  alter table notes add constraint fk_notes01 foreign key (employee_id) references employee(id) on delete cascade;  alter table notes add constraint fk_notes02 foreign key (addedbyemployee_id) references employee(id); 

so, employee can have 1 or more notes , each note added employee.

i've modelled in entity framework in note.cls:

public int id { get; set; }  public string text { get; set; }  public virtual employee employee { get; set; }  public virtual employee addedbyemployee { get; set; } 

and employee.cls:

public int id { get; set; }  public virtual list<note> notes { get; set; } 

(i'm assuming have reference notes collection employee.cls once).

currently, when try load model a:

invalid column name 'employee_id1'.

and it's running following sql:

select  [extent1].[id] [id],  [extent1].[text] [text],  [extent1].[employee_id] [employee_id],  [extent1].[addedbyemployee_id] [addedbyemployee_id],  **[extent1].[employee_id1] [employee_id1]** [notes] [extent1] ([**extent1].[employee_id1]** not null)        , (**[extent1].[employee_id1]** = 684) 

so it's expecting column called employee_id1 i'm not sure making expect that. suspect there other issues after one. if has ideas, i'd love hear them.

since field names not follow default convention, you'll need tell ef fields should used 2 employee navigation properties.

[foreignkey("employee_id")] public virtual employee employee { get; set; }  [foreignkey("addedbyemployee_id")] public virtual employee addedbyemployee { get; set; } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -