我第一次使用Ado.net实体框架,需要在将记录插入数据库之前检查该记录是否存在。最好是搜索AuthodSSID是否存在,而不是键(AuthorID)。我使用的是VS2010,Framework 4。System.Data.Entity是3.5.0.0。
我已经搜索了,但没有找到此问题的答案。
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
}