我想在我的数据库中添加一本新书,但它不起作用。
class Program
{
static void Main(string[] args)
{
Knigi_DataEntities entities = new Knigi_DataEntities();
Books newBook = new Books();
newBook.Name = "C# 4 How-to";
newBook.Published = 2010;
newBook.Book_ID = 4;
entities.AddToBooks(newBook);
entities.SaveChanges();
Knigi_DataEntities ent = new Knigi_DataEntities();
foreach (var book in ent.Books)
{
Console.WriteLine("{0}, {1} {2} ",
book.Book_ID, book.Name, book.Published);
}
}
}
但是当我关闭时//entities.SaveChanges();
,控制台会显示我的数据库中已经存在的对象。因此问题是由entities.SaveChanges();
请告诉我,如何添加新对象?