实体框架不将数据条目保存在数据库中
首先我应该提到这个问题只发生在 Windows 窗体应用程序中,并且 Web 模式下的相同程序(例如 MVC3)可以完美运行。
几天前,我使用 Visual Studio 2010 Ultimate 和 SQL Express 数据库编写了一个非常简单的 Windows 窗体程序。我通过选择“添加”>“添加数据库”新项目>基于服务的数据库和基于该数据库的实体数据模型以同样的方式。我使用实体框架向表中添加一些新记录。我之前用 VS 2008 SP1 做过这样的事情,没有问题,所以我也做了同样的事情。程序编译并运行没有错误,我输入了一些新数据。退出程序后我回到数据库,什么也没有发生。我输入的任何信息均未保存。 我一步步调试程序,一切正常。 下面的代码与一个具有上述问题的非常简单的程序相关。数据库有一张表(书):
namespace Book
{
public partial class BookForm : Form
{
BookDatabaseEntities db = new BookDatabaseEntities();
public BookForm()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
Book bookToCreate = new Book();
bookToCreate.Id = Guid.NewGuid();
bookToCreate.Title = titleTextBox.Text;
db.Books.AddObject(bookToCreate);
db.SaveChanges();
}
}
}
如果有人帮助我,我将非常感激。提前致谢。
......................
编辑后:
namespace Book
{
public partial class BookForm : Form
{
//BookDatabaseEntities db = new BookDatabaseEntities();
public BookForm()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
var db = new BookDatabaseEntities();
var bookToCreate = db.Books.CreateObject();
//Book bookToCreate = new Book();
bookToCreate.Id = Guid.NewGuid();
bookToCreate.Title = titleTextBox.Text;
db.AcceptAllChanges();
db.Books.AddObject(bookToCreate);
db.SaveChanges();
}
}
}
At first I should mention that this problem only occurs in windows forms applications and the same program in web mode for example with MVC3 works perfect.
Some days ago I wrote a very simple windows form program using Visual studio 2010 ultimate with a SQL Express database. I added the database by choosing Add > New item > Service-Based database and an entity data model based on this database in same way. I used Entity framework for adding some new records to tables. I had done such thing with VS 2008 SP1 before with no problem so I did the same. The program compiled and ran with no errors and I entered some new data. after exiting the program I came back to the database and nothing was happened. None of information I had entered had been saved.
I debug the program step by step and everything was alright.
The code below is related to a very simple program with mentioned problem. Database has one table (book):
namespace Book
{
public partial class BookForm : Form
{
BookDatabaseEntities db = new BookDatabaseEntities();
public BookForm()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
Book bookToCreate = new Book();
bookToCreate.Id = Guid.NewGuid();
bookToCreate.Title = titleTextBox.Text;
db.Books.AddObject(bookToCreate);
db.SaveChanges();
}
}
}
I'll be very grateful if anyone help me. Thanks in advance.
.................
After editing:
namespace Book
{
public partial class BookForm : Form
{
//BookDatabaseEntities db = new BookDatabaseEntities();
public BookForm()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
var db = new BookDatabaseEntities();
var bookToCreate = db.Books.CreateObject();
//Book bookToCreate = new Book();
bookToCreate.Id = Guid.NewGuid();
bookToCreate.Title = titleTextBox.Text;
db.AcceptAllChanges();
db.Books.AddObject(bookToCreate);
db.SaveChanges();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,经过大量搜索和询问,我在 MSDN 论坛中找到了解决方案,感谢 Patrice Scribe
你可以看到它
此处
Finally after lots of searching and asking I found the solution in MSDN forums thanks to Patrice Scribe
You can see it
here
试试这个:
编辑:
我在生产中的类库(我的DAL)中有这段代码,它工作正常:
请注意,事实上我没有分配PK(DataId )因为它将由数据库分配,所以我将其返回给调用者,以便调用 save 方法的人可以知道自动生成的 ID,以防需要它。
try this:
Edit:
I have this code in a class library (my DAL) in production and it works fine:
notice that in fact I do not assign the PK (DataId) because it will be assigned by the database, i return it to the caller so who has invoked the save method gets to know the auto generated ID, in case it needs it.