In Linq to SQL, why does assigning a linked object create a ChangeSet insert?

Why is Insert (new address) added to the DataContent ChangeSet and how can I stop it from doing this?

var db = new DataClasses1DataContext();
var a = new Address();
a.StateProvince = db.StateProvinces.First();
Console.WriteLine(db.GetChangeSet().Inserts.Count);
+3
source share
3 answers

Address a = new address ();

This creates a new instance of Address. It has nothing to do with datacontext.

db.StateProvinces.First ();

This loads an instance of StateProvince. The instance is tracked by the datacontext that loaded it (db). db is waiting for notification of changes from this instance.

a.StateProvince =

. "StateProvince", , "" StateProvince. StateProvince . db, - db , .

- db :

db.ObjectTrackingEnabled = false;

PS. var is awesome, -, .

+2

, . , . , , :

Address.StateProvinceID = db.StateProvinces.First().StateProvinceID;

. LINQ to SQL, PLINQO (http://plinqo.com/default.aspx?AspxAutoDetectCookieSupport=1).

+2

, "StateProvince" , , , , , StateProvince. assoication, , , .

:

DataClasses1DataContext db = new DataClasses1DataContext();
db.ObjectTrackingEnabled = false;
Address a = new Address();
a.StateProvince = db.StateProvinces.First();

, var; -)

+1

Source: https://habr.com/ru/post/1755492/


All Articles