实体框架 SaveChanges() 不写入数据库
当我尝试使用 SaveChanges() 将实体写入数据库时,调用成功(返回 1),但实际上并未写入数据库。此外,通过添加 savingchanges 事件处理程序,我可以看到该项目实际上确实通过了具有正确属性的保存事件(具体来说,一个对象更改了状态,它是我发送的新实体)。当我阅读数据库后记时,没有新的数据行。没有错误或抛出异常...
CommunityEF.Community c = new CommunityEF.Community();
c.IdNumber = 0; //set key
c.Name = "Test";
CommunityEF.CommunitySystemContainer cc = new CommunityEF.CommunitySystemContainer();
cc.Communities.AddObject(c); // also tried "cc.AddToCommunities(c);"
int result=cc.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
//result is always 1
我已经评论过类似的文章,但大多数都缺少一行或不正确(甚至乱序)。在这种情况下,我可以看到它从头到尾成功地通过系统,但它实际上并没有进入数据库。
由于在另一篇文章中提出了这个问题,所以这是我的连接字符串: 元数据=res://*/CommunityEF.CommunitySystem.csdl|res://*/CommunityEF.CommunitySystem.ssdl|res://*/CommunityEF.CommunitySystem.msl;provider=System.Data.SqlServerCe.3.5 ;提供者连接字符串="数据源=|DataDirectory|\DB\Database1.sdf"" providerName="System.Data.EntityClient" />
另外,这应该不重要(但我知道它确实如此),但我使用的是 VS2010 Express,它是一个 Windows 应用程序,而不是一个 Web 应用程序,
谢谢 。请提前就此事向您寻求帮助。
When I try to write an entity to the database with SaveChanges(), the call succeeds (returns 1), but does not actually write to the database. Furthermore, by adding a savingchanges event handler, I am able to see that the item does in fact go through the save event with the correct properties (specifically, one object changed state and it is the new entity I sent). When I read the database afterword, there are no new data rows. There are no errors or thrown exceptions...
CommunityEF.Community c = new CommunityEF.Community();
c.IdNumber = 0; //set key
c.Name = "Test";
CommunityEF.CommunitySystemContainer cc = new CommunityEF.CommunitySystemContainer();
cc.Communities.AddObject(c); // also tried "cc.AddToCommunities(c);"
int result=cc.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
//result is always 1
I have review similar articles, but most of them have one line missing or incorrect (or even out of order). In this case, I can see that it is passing through the system successfully from start to end, but it doesn't actually end up in the database.
Since it was asked in another article, here is my connection string:metadata=res://*/CommunityEF.CommunitySystem.csdl|res://*/CommunityEF.CommunitySystem.ssdl|res://*/CommunityEF.CommunitySystem.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=|DataDirectory|\DB\Database1.sdf"" providerName="System.Data.EntityClient" />
Also, it shouldn't matter (but I know it does), but I am using VS2010 Express and it is a windows application, not a web application.
Thank you in advance for help on this matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定,但您的 SaveOptions 枚举可能是问题所在。尝试只调用
而不进行枚举。
Not sure but your SaveOptions enumeration may be the problem. Try just calling
without the enumeration.