我第一次使用Ado.net實體框架,並且在將其插入數據庫之前需要檢查此記錄是否存在。最好我會搜索是否存在AuthodSSID而不是密鑰(AuthorID)。我使用的是VS2010,Framework 4,System.Data.Entity是3.5.0.0。在插入新記錄之前檢查記錄的存在
我使用Google搜索,但沒有找到這個問題的答案。
PublishingCompanyEntities publishContext;
publishContext = new PublishingCompanyEntities();
private void createNew_Click(object sender, EventArgs e)
{
Author newAuthor = new Author();
newAuthor.FirstName = firstName.Text;
newAuthor.LastName = lastName.Text;
newAuthor.AuthodSSID = 20;
newAuthor.AuthorID = 10
//Check if record exist here
publishContext.AddToAuthor(newAuthor);//insert if does not exist
}
舊的但仍然看起來像一個DUP;更多信息可以在http://stackoverflow.com/questions/1802286/best-way-to-check-if-object-exists-in-entity-framework – Nenotlep 2013-04-25 06:47:46
找到[最好的方法來檢查對象是否存在實體框架?](https://stackoverflow.com/questions/1802286/best-way-to-check-if-object-exists-in-entity-framework) – 2017-12-26 14:00:18