实体框架插入不工作 [英] Entity Framework insert doesn't work

查看:433
本文介绍了实体框架插入不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的Windows窗体项目实体框架。

只要我draged我的表从数据源选项卡在我的形式,它生成了我一个的DataGridView和的BindingSource。

数据绑定成功后,当我跑项目,我可以看到的DataGridView填写正确数据,我可以从更新的DataGridView任何单元格的值。

问题是我不能插入任何行我的DataGridView。

下面是一些codeS,我写的,希望解决问题,它是有用的:

  Teachers_DBEntities上下文=新Teachers_DBEntities();

    私人无效SaveItem_Click(对象发件人,EventArgs的)
    {
        context.SaveChanges();
        的MessageBox.show(保存成功);
    }

    私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        teachers_tableBindingSource.DataSource = context.teachers_table.ToList();
    }
 

Upates征求意见

我测试了我的的BindingSource,发现它成功地明白,如果新记录插入在DataGridVeiw,但变化不会在数据库应用的时候我叫 context.savechanges();

context.savechanges()工作正常,当我更新细胞,但是当我尝试插入一个新的记录在我的的DataGridView这是行不通的。

在我的EDMX文件I正确映射所有列和主键 StoreGeneretedPattern 属性设置为身份实体键属性设置为。其自动递增属性在我的SQL Server数据库文件设置为

任何帮助是AP preciated。

解决方案
  

context.savechanges()时,我更新细胞正常工作,但是当我尝试插入一个新的记录在我的的DataGridView。

这是行不通的

你所说的这是行不通的呢?新的记录不会出现在数据库中?当然不会。

在这一行

  teachers_tableBindingSource.DataSource = context.teachers_table.ToList();
 

你打破数据源的连接上下文。插入任何新的项目将被插不进 teachers_table ,但进入列表你在它创建的​​。

I have a very simple windows form project with Entity Framework.

Simply I draged my tables from "Data Source" tab in my "form" and it generate for me a "DataGridView" and a "BindingSource".

Data bound successfully and when I run project I can see "DataGridView" filled with data correctly and I can update any cells value from "DataGridView".

Problem is I can not insert any rows in my "DataGridView".

Here is some codes that I wrote hope it be useful to solve problem:

    Teachers_DBEntities context = new Teachers_DBEntities();

    private void SaveItem_Click(object sender, EventArgs e)
    {
        context.SaveChanges();
        MessageBox.Show("saved successfully");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        teachers_tableBindingSource.DataSource = context.teachers_table.ToList();
    }

Upates for comments

I tested my "BindingSource" and found that it successfully understand if new record insert in "DataGridVeiw", but changes won't apply in database when I call context.savechanges();

context.savechanges() works fine when I update a cell, but it doesn't work when I try to insert a new record in my "DataGridView".

In my edmx file I mapped all columns correctly and primary key StoreGeneretedPattern property is set to Identity and its Entity Key property is set to true. Its auto-increment property in my SQL Server database file is set to true.

any help is appreciated.

解决方案

context.savechanges() works fine when I update a cell, but it doesn't work when I try to insert a new record in my "DataGridView".

What do you mean by "it doesn't work"? New record does not appear in database? Of course it won't.

In this line

teachers_tableBindingSource.DataSource = context.teachers_table.ToList();

you're breaking DataSource's connection to context. Any new item inserted into it will be inserted not into teachers_table, but into List you created over it.

这篇关于实体框架插入不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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