linq to sql - How to modify linqtosql entityref objects in handcoded MVC model? -


i trying set own mvc model rather letting environment create 1 via graphic designer tool. had hoped make easier keep separate repositories parts of model space far has caused me nothing grief.

the first problem ran entityref classes had updated via selectlist control in view. managed work adding interal id field every entityref designer.cs do. however, has made model class quite bit more complex code below demonstrates.

unfortunately, run problem when want explicitly update of entities in controller. if manually set id field, update dropped, if change entity exception while saving.

my model

[table(name = "dbo.jobs")] public class job {     [column(isprimarykey = true, isdbgenerated = true, autosync = autosync.oninsert)]     public int jobid { get; set; }      internal string _companyid;   // string legacy reasons     [column(storage = "_companyid")]     public string companyid{         { return _companyid}         set {             if ((_companyid != value)) {                 if (_mittlerorg.hasloadedorassignedvalue) {                     throw new system.data.linq.foreignkeyreferencealreadyhasvalueexception();                 }                 _companyid = value;             }         }     }      internal entityref<company> _company;     [association(storage = "_company", thiskey = "companyid", otherkey = "companyid", isforeignkey = true)]     public company company {         { return _company.entity; }         set {             organization previousvalue = this._company.entity;             if ((previousvalue != value) || (_company.hasloadedorassignedvalue == false)) {                 if ((previousvalue != null)) {                     _company.entity = null;                 }                 _company.entity = value;                 if (value != null) {                     _companyid = value.organizationid;                 } else {                     _companyid = default(string);                 }             }         }     }      // contact depends on choice of company , should set     // inside action method once company determined.     internal string _contactid;     [column(storage = "_contactid")]     public string contactid {         { return _contactid; }         set {             if ((_contactid != value)) {                 if (_contact.hasloadedorassignedvalue) {                     throw new system.data.linq.foreignkeyreferencealreadyhasvalueexception();                 }                 _contactid = value;             }         }     }      internal entityref<user> _contact;     [association(storage = "_contact", thiskey = "contactid", otherkey = "userid", isforeignkey = true)]     public user contact {         { return _contact.entity; }         set {             user previousvalue = this._contact.entity;             if ((previousvalue != value) || (_contact.hasloadedorassignedvalue == false)) {                 if ((previousvalue != null)) {                     _contact.entity = null;                 }                 _contact.entity = value;                 if (value != null) {                     _contactid = value.userid;                 } else {                     _contactid = default(string);                 }             }         }     }           } 

the edit function causes problems here. if step though in debugger see fi.contactid gets updated not committed db.

   [httppost]     public actionresult edit(int id, formcollection collection) {         var user = userrep.fetchbylogin(user.identity.name);         var job = jobrep.fetchbyid(id);           try {             var oldvalue = job.companyid;             updatemodel(job, "job");              if (oldvalue != job.companyid) {                 if (job.companyid != null) {                     job.contactid = orgrep.fetchbyid(job.companyid).defaultcontactid;                 } else {                     job.contactid = default(string);                 }             }             firep.save();              return redirecttoaction("index");         } catch (exception e) {         }     } 

any idea how pesky entityrefs behave? searched , down internet model layer examples seem cover simplest relationships only. should chuck model in favor of managing references manually though id fields.

cheers, duffy

update: never got piece of code work robustly ended switching letting visual studio generate datacontext via drag , drop graphical designer.

i still struggle bit fixing names on relationship links after update of db schema (i name relationships in db designer tool seems ignore names) since discovered db.designer.cs file can opened in xml editor rather gui, job got lot easier.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -