[英]Exception “The specified entity is not contained in this EntitySet.” but Entity is contained
我有一個異常“ The specified entity is not contained in this EntitySet.
” 但是當我查看數據源時,看到的是我的實體。
這是代碼:
Class1 item = (Class1)dg.SelectedItem;
if (item!= null)
{
Class2 item2= new Class2();
item2.Att1= item.Att1;
item2.Att2= item.Att2;
item2.Att3= item.Att3;
MyDataSource.DataView.Remove(item2);
}
我想刪除數據源的對象,該對象在我的數據網格中具有所選項目的屬性。
當我在DataSource上觀看時,我的item2出現了,那么為什么不能從DataSource中刪除它? 相同的類,相同的屬性。 我不明白
我發現了,並且效果很好:
if (item!= null)
{
Class2 myPiece = MyDataSource.DataView.OfType<Class2>().Where(OrEs => OrEs.Att1 == item.Att1).FirstOrDefault() ;
MyDataSource.dataView.Remove(myPiece);
}
基本上,您需要刪除相同的項目,而您正在創建一個新的項目,這會產生問題。
應該是這樣的
Class1 item = (Class1)dg.SelectedItem;
if (item!= null)
{
MyDataSource.DataView.Remove(item );
}
Item2不屬於您的數據源。
具有相同類型和屬性的兩個對象不一定意味着它們相同,同樣,它們應該位於相同的存儲位置上。
您需要在數據源中找到與相同屬性匹配的項目,然后將其刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.