调用ObjectContext.SaveChanges()后,数据库更改不会持久 [英] Database changes do not persist after ObjectContext.SaveChanges() is called

查看:114
本文介绍了调用ObjectContext.SaveChanges()后,数据库更改不会持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用实体框架将记录插入到表中。我做了我的数据库(mdf文件),从数据库(edmx)中创建一个模型,现在我试图插入一个记录。

I am trying to insert a record into a table using an Entity Framework. I made my database (mdf file), made a model from the database (edmx), and now I am trying to insert a record.

Users Brad = new Users
{
    UserInitials="BCH",
    UserName="Brad"
};
using (WeighProgramDatabaseEntities wDB = new WeighProgramDatabaseEntities())
{
    wDB.Users.AddObject(Brad);

    int res = wDB.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
    List<Users> lwt = wDB.Users.ToList();  //call to look at the inserted data
}

当我按照这个过程出现一切正常我的 wDB.Users.ToList(); 返回一个我刚刚插入的记录。但是,如果我再次运行我的代码,删除插入的东西,只是调用查询(只留下)列表<用户> lwt = wDB.Users.ToList(); 我的查询不返回记录。我应该怎么做不同,让我的变化持续下去?为什么我的记录离开?

When I follow this procedure it appears everything works fine. My wDB.Users.ToList(); returns one record which is the one I just inserted. However, if I run my code again, removing the insert stuff and just call the query (leaving just) List<Users> lwt = wDB.Users.ToList(); my query returns no records. What should I be doing differently to get my changes to persist? Why did my records "go away"?

WeighProgramDatabaseEntities 继承自 ObjectContext

这是我第一次尝试使用EF的东西,所以我的一些动词可能不正确,我可能没有包含重要的东西 - 让我知道

This is my first attempt at using EF stuff so some of my verbiage might not be correct and I might not have included something important - let me know.

编辑:

我想我的问题是我的插入/更新/删除功能未映射。但我不知道如何创建一个存储过程,以便他们映射到...

I think my problem is that my Insert/Update/Delete functions are not mapped. But I'm not sure how to create a stored procedure for them to map to...

Edit2:

好的,呃,我想出了存储过程映射。现在我有一个INSERT映射,我调用 AddObject 它将实际插入。

Ok, phew, I figured out the stored procedure mapping. and now that I have an INSERT mapped, and I call AddObject it will actually be inserted.

比我想象的要复杂一点。例如,如果我右键单击我的数据库资源管理器中的表,并选择新建查询,它将带给我一个伟大的查询设计器窗口,让我创建插入,选择,更新...但没有保存按钮!这是灰色的我需要从另一个路由编写它们,不让我通过GUI生成插入/删除/更新。我猜这是好的,因为他们没有多少文字,但仍然有点麻烦。

A little more complicated than I would have expected. For example if I right click on a table in my Database Explorer and select New Query it brings me to a great query designer window which let's me create Inserts, Selects, Updates... but there is no Save button! It's grey out. I need to write them from another route with does not let me generate Inserts/Deletes/Updates via a GUI. Which is ok I guess because they are not much text, but still a bit of a hassle.

推荐答案

我认为你的 ConnectionString 指向解决方案的Debug / Release文件夹中的mdf,每次构建应用程序时,该文件可能会被覆盖,这就是为什么您认为更改不会持久存在。尝试将 ConnectionString 设置为指向项目文件夹中的mdf(项目实际引用的)。

I think that your ConnectionString points to the mdf in the Debug/Release folder of your solution, and that file is probably overwritten each time you build your application that's why you think your changes are not persisted. Try setting the ConnectionString to point to the mdf in the project folder (the one that is actually reference by the project).

这篇关于调用ObjectContext.SaveChanges()后,数据库更改不会持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆