作爲實體框架代碼的一些測試的一部分首先,我正在測試更改跟蹤。在一個小的測試數據庫中,我有一個表兩輛車,我對運行一個測試方法:EF爲什麼在SaveChanges上讀取我的Unchanged對象?
Debug.WriteLine("Reading cars...");
var cars = context.Cars.ToArray();
Debug.WriteLine("Updating top speed of first car...");
Debug.WriteLine(string.Format("Type of car[0] is {0}", cars[0].GetType().ToString()));
cars[0].TopSpeed = 260;
Debug.WriteLine("Saving changes...");
context.SaveChanges();
我已經添加了跟蹤輸出到Car
類的TopSpeed
和Brand
屬性的getter和setter看他們如何訪問。 TopSpeed
是int?
和Brand
是Brand
實體的導航屬性。運行上面的代碼給出下面的輸出。
Reading cars... Setting TopSpeed to 210 for ABC123. Car: Getting TopSpeed for ABC123. Setting TopSpeed to 250 for XYZ987. Car: Getting TopSpeed for XYZ987. Updating top speed of the first car... Type of car[0] is System.Data.Entity.DynamicProxies.Car_18E3E11297DC48759312BDF1C2FFEBE9F19BAE5D487CED2A9781A6CA730071EA Setting TopSpeed to 260 for ABC123. Saving changes... Car: Getting Brand for ABC123. Car: Getting Brand for XYZ987. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for XYZ987. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123. Car: Getting TopSpeed for ABC123.
該對象的類型是用於更改跟蹤的EF動態代理。當調用SaveChanges()
時,讀取未更改的XYZ987汽車的屬性。我認爲更改跟蹤會導致EF只讀取已知會更改的對象,或者我錯過了某些內容?是否還需要添加其他功能才能啓用更改跟蹤?