c# - Updating a mappin reference in EF 4.0 -


to begin i'm not sure if it's called mappin reference. have 2 tables important information , 1 table in between them. follows:

  create table customer (       id int not null identity(1,1), -- pk       number varhcar(255) not null,   ) ....    create table category (       id int not null identity(1,1), -- pk       type int not null,             -- defines different category types       number varchar(50) not null,       name varchar(255) not null   ) ....    create table customercategories (       fk_customer int not null, -- references customer       fk_category int not null  -- references category   ) .... unique (fk_customer, fk_category) 

in model, customer can have several categories (as long category.type different).

it might this:

customer:

id| number

3 | 10000

category:

id|type|number |name

52| 1 | prio_h |high

53| 2 | reg | region south

customercategories:

fk_customer | fk_category

3 | 52

3 | 53

in c# application put categories in grid, users can change different category types based on drop down list.

i use ef4 object context.

in application customer model this

  interface icustomer    {      int id {get;}      string number {get;}      category priority {get;set;}      category region {get;set;}   } 

and implementation

 partial class customer : icustomer  {      id... // not important right      number... // not important right      public category priority      {          { return this.customercategory.where(c => c.type == 1).select(c2 => c2.category).firstordefault<category>(); } // getter works          set {}      }  } 

i have problems setting value, have tried

set {this.customercategory.where(c => c.type == 1).select(c2 => c2.category) == value;} 

and 1 says : cannot convert type model.category ienumerable

so have tried explicitly select value doing

set {this.customercategory.where(c => c.type == 1).select(c2 => c2.category).firstordefault<category>() == value;} 

and says : left hand side of assignment must variable, property or indexer.

does know how update such references using ef4 , object context?

thanks!

you trying assign value result of iqueryable. should work:

set  {     var cc = context.customercategory.where(c => c.type == 1).firstordefault();     if (cc!=null)     {         cc.category =  value;         //or cc.categoryid = value.id; if have foreign keys exposed         //context.savechanges(); if need changes saved in database immediately.     } } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -