c# - Entity Framework not Lazy Loading FK Collection on Add -


i have following database structure entity framework (version 5) implementation has been working years, getting slower , slower because of kind of circular referencing issues.

[table("sensors", schema = "ems")] public class sensor {     public sensor()     {         sensorsamples = new list<sensorsample>() icollection<sensorsample>;     }      [key]     public int id { get; set; }      [required, maxlength(128)]     public string name { get; set; }      [maxlength(256)]     public string description { get; set; }      [maxlength(128)]     public string location { get; set; }      [required]     [maxlength(15)]     public string ipaddress { get; set; }      [required]     public int port { get; set; }      [required]     public bool enabled { get; set; }      [required, foreignkey("type")]     public int sensortypeid { get; set; }      public virtual sensortype type { get; set; }      [required, foreignkey("network")]     public int sensornetworkid { get; set; }      public virtual sensornetwork network { get; set; }      public virtual icollection<sensorsample> sensorsamples { get; set; } }  [table("sensorsamples", schema = "ems")] public class sensorsample {     public sensorsample()     {         sampledata = new list<sampledata>() icollection<sampledata>;     }      [key]     public int id { get; set; }      [required, foreignkey("sensor")]     public int sensorid { get; set; }      public virtual sensor sensor { get; set; }      [required]     public datetime sampletime { get; set; }      [required]     public virtual icollection<sampledata> sampledata { get; set; } }  [table("sampledata", schema = "ems")] public class sampledata {     public sampledata()     {     }      [key]     public int id { get; set; }      [required, foreignkey("datatype")]     public int sampledatatypeid { get; set; }      public virtual sampledatatype datatype { get; set; }      [required, foreignkey("unit")]     public int sampleunitid { get; set; }      public virtual sampleunit unit { get; set; }      [required, foreignkey("sample")]     public int sensorsampleid { get; set; }      public virtual sensorsample sample { get; set; }      [maxlength(128)]     public string value { get; set; } } 

when add new sensorsample using following code, takes forever first 1 added because instantiates sensorsample , adds samples collection on sensor instance.

sensor sensor = getsensor(1); sensorsample sample = new sensorsample(); sample.sampletime = d.timestamp; sample.sensorid = sensor.id; sensor.sensorsamples.add(sample); 

how can add sample sensorsamples on sensor without instantiating enitire collection of existing sensorsamples? have autodetectchangesenabled set false , delay detectchanges until right before savechanges. makes no difference. have not turned off lazyloading doesn't seem kicking in expect in situation. think requirements met lazyloading such having public parameterless constructors. have missed something? ideas why happening? thanks.

i have idea why happening: when ef loads database, not return type, instead returns class derives yours. if class has virtual properties ef can deduce relationships, class implements virtual properties upon access load associated objects storage. such operation can become quite long more , more foreign keys associated instance.

in order avoid this, create sensor sample instance, map between business , storage models , set foreign key associates sample sensor. once done, add sample proper dbset in dbcontext , save changes.

it along lines of:

var sample = new sensorsample();  .. map properties , values business storage model  //map sensor foreign key sample sample.sensorid = sensor.id;  context.sensorsamples.add(sample); context.savechanges(); 

sidenote:

do refrain using lazy virtual collection features in ef unless know resulting set bounded.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -