私はちょっと困っているので、ここで説明します。テーブルに行を挿入する foreach ループがあります。しかし、挿入する前に、同じ ID の行が既に存在するかどうかを確認し、存在する場合は、他のプロパティ (列) が同じ値であるかどうかを確認したいと考えています。
これが私がすることです:
var sourceList = LoadFromOtherDataBase();
var res = ListAll(); // Load all rows from database which were already inserted...
foreach (Whatever w in sourceList)
{
Entry entry = new Entry();
entry.id = w.id;
entry.field1 = w.firstfield;
entry.field2 = w.secondfield;
//so on...
//Now, check if this entry was already inserted into the table...
var check = res.Where(n => n.id == entry.id);
if (check.Count() > 0)
{
var alreadyInserted = res.Single(n => n.id == entry.id);
//Here, I need to see if 'alreadyInserted' has the same properties as 'entry' and act upon it. If same properties, do nothing, otherwise, update alreadyInserted with entry's values.
}
else
{
//Then I'll insert it as a new row obviously....
}
}
私は Object.Equals() について考えましたが、Entity Framework は、entryで null に設定されているalreadyInsertedの null 以外のEntityKeyプロパティを作成します。それが機能しない理由だと思います。EntityKeyを null に設定することはできません。
すべてのプロパティ (実際には私の場合は 25 以上) を手動でチェックせずにこれを行う方法についてのアイデアはありますか?