2024年1月31日 星期三

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new
var newmodel = _contextXXX.CreateProxy<yyy>();
...
_contextXXX.yyy.Add(newmodel);
await _contextXXX.SaveChangesAsync();
var aaa=newmodel.xxx.yyy; 

或者直接reload 物件
await _contextXXX.Entry<yyy>(newmodel).ReloadAsync();

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...