我注意到,由於某種原因,我的對象既不是來自查詢,也沒有添加ObjectContext.MySet.AddObject(myObj)以某種方式附加到ObjectContext。實體框架4:是否自動附加對象?
foreach (SomeClass someObject in someSet)
{
//it says an object is added...
MessageBox.Show(someObject.EntityState.ToString());
foreach (SomeProperty someProperty in someObject.SomeProperty)
{
//type is given above
someObject.someProperty = type;
}
var existing = from o in db.SomeObjectSet
where o.Name == someObject.Name
select o;
if (existing.Count() == 0)
{
db.SomeObjectSet.AddObject(someObject);
}
//apparently I have to detach them because
//they are already attached for some reason
else
{
db.Detach(vulnerability);
}
}
db.SaveChanges();
請注意,someSet並非來自它來自不同類型源的數據庫,說一個文本文件。
通常,我永遠不需要分離一個對象,因爲它沒有連接,但在這種情況下並不明顯。
什麼是'漏洞'? 'someObjects'之間有任何關係嗎? – 2011-03-22 10:45:44
什麼是'SomeObjectSet'? – 2011-03-22 10:51:06
你能否更清楚問題是什麼? – Xhalent 2011-03-22 10:53:44