c# - LINQ One-to–Zero-or-One is not UPDATING the record -


i'm trying update database one-to–zero-or-one using linq. (i'm using code first). line "system.diagnostics.debug.write(await databaseconn.savechangesasync());" return value 0 instead of 1.

it supposed return 1 because i'm updating sog of currenttruck "currenttruck.sog = newsog;"

details

databases: truck & sog

scenario : truck has 1 sog item, each truck should have 1 sog item, sog without parent, in other words can have:

truck -> sog & truck -> without sog & sog-> truck & sog -> without truck. truck model

[key] [required] [display(name = "truck id")] public string truck_id { get; set; }  [required] [stringlength(10)] [display(name = "rego")] public string truck_rego { get; set; }  public int? subc_id { get; set; }  public virtual subcontractor subcontractor { get; set; }  public virtual sog sog { get; set; }  public virtual icollection<truckservices> truckservices { get; set; } 

sog model

[key, foreignkey("truck")] public string truck_id { get; set; }  [required] public string sog_serialnumber { get; set; }  public virtual truck truck { get; set; } 

i need remove current sog item truck , add new sog item truck, in other words, truck has sog 1 , truck b has sog 2, need remove connection between truck b & sog 2, , remove connection between truck & sog 1 , after add connection between truck sog 2.

i've used tutorial (http://www.entityframeworktutorial.net/entityframework4.3/update-one-to-one-entity-using-dbcontext.aspx) doesn't work, i've tried code below nothing changes.

public async task<actionresult> truckaccount_sog(string id, string _sog_imei) {     truck   currenttruck        = await databaseconn.trucks.firstordefaultasync(r=> r.truck_rego.equals(id));      sog     currentsog          = await databaseconn.sogs.firstordefaultasync(r=> r.truck_id.equals(currenttruck.truck_id));     sog     newsog              = await databaseconn.sogs.firstordefaultasync(r=> r.sog_imei.equals(_sog_imei));      databaseconn.entry(currenttruck).state  = entitystate.modified;     databaseconn.entry(currenttruck).state  = entitystate.detached;     databaseconn.entry(currentsog).state    = entitystate.detached;     databaseconn.entry(newsog).state        = entitystate.detached;      currenttruck.sog = newsog;      system.diagnostics.debug.write(await databaseconn.savechangesasync());      return null; } 

is relationship correct?

how can update record?

does has example how use save/update ? i've found one-to-zero-or-one works

ps: didn't check null exception because while i'm testing , vars valid.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -