实体框架不将数据条目保存在数据库中

发布于 2024-12-03 04:55:35 字数 1696 浏览 7 评论 0原文

首先我应该提到这个问题只发生在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-12-10 04:55:35

最后,经过大量搜索和询问,我在 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

︶ ̄淡然 2024-12-10 04:55:35

试试这个:

db.Attach(bookToCreate);
db.SaveChanges();

编辑:

我在生产中的类库(我的DAL)中有这段代码,它工作正常:

using (var dbContext = new DbEntities())
    {
        var job = dbContext.RiskToolJob.CreateObject();

        job.AnalysisDataID = analysisDataID;

        job.JobRmsAnalysisID = RMSAnalysisID;
        job.UserName = userName;

        job.JobCreated = DateTime.UtcNow;

        dbContext.RiskToolJob.AddObject(job);

        dbContext.SaveChanges();

        return job.DataId;
    }

请注意,事实上我没有分配PK(DataId )因为它将由数据库分配,所以我将其返回给调用者,以便调用 save 方法的人可以知道自动生成的 ID,以防需要它。

try this:

db.Attach(bookToCreate);
db.SaveChanges();

Edit:

I have this code in a class library (my DAL) in production and it works fine:

using (var dbContext = new DbEntities())
    {
        var job = dbContext.RiskToolJob.CreateObject();

        job.AnalysisDataID = analysisDataID;

        job.JobRmsAnalysisID = RMSAnalysisID;
        job.UserName = userName;

        job.JobCreated = DateTime.UtcNow;

        dbContext.RiskToolJob.AddObject(job);

        dbContext.SaveChanges();

        return job.DataId;
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文