我搜索了很多关于我的问题,但我还没找到任何东西!
我创建了一个数据库和一个表,然后将我的Form与EntityFramework连接到那些..
当我输入数据信息并单击“添加”按钮时,数据库不会添加新行......
(这些是我的代码)
主要代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using System.Diagnostics;
namespace WindowsFormsApplication6
{
public partial class BuyForm : Form
{
public BuyForm()
{
InitializeComponent();
}
private void BuyForm_Load(object sender, EventArgs e)
{
}
notebookEntities database = new notebookEntities();
private void buyGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void BuyForm_Load_1(object sender, EventArgs e)
{
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
buytbl tbl = new buytbl()
{
name = bnameTextBox.Text,
price = int.Parse(bpriceTextbox.Text),
date = dateTimePicker1.Value,
deadline = dateTimePicker2.Value,
buyername = bbuyerTextBox.Text,
count = int.Parse(bcountTextBox.Text)
};
if (bpriceTextbox == null)
{
String a = "THE FIELD IS NULL!";
MessageBox.Show(a);
}
database.buytbls.Add(tbl);
dataGridView1.DataSource = database.buytbls.ToList();
}
}
}
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="masterEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SMQ2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="notebookEntities" connectionString="metadata=res://*/namadModel.csdl|res://*/namadModel.ssdl|res://*/namadModel.msl;provider=System.Data.SqlClient;provider connection string="data source=smq2;initial catalog=notebook;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
谢谢你们!...
答案 0 :(得分:0)
当你这样做时
database.buytbls.Add(tbl);
您将实体添加到数据库上下文
但是要提交你添加的实体,你需要做
database.SaveChanges();
答案 1 :(得分:0)
与您的问题没有直接关系,但非常重要:您不应该在多个操作上重复使用数据库上下文。在button1_Click内,创建新的数据库上下文实例,向其添加一个对象,然后调用SaveChanges。然后,数据将出现。
现在回答。必须在这两行之间调用SaveChanges:
database.buytbls.Add(tbl);
database.SaveChanges(); // <---- This line is missing
dataGridView1.DataSource = database.buytbls.ToList();
数据不出现的原因在于DbContext的工作方式。设置DataSource的最后一行不会获取DbContext中包含的数据,而是在数据库上运行另一个查询。并且,由于未调用SaveChanges,因此数据尚未存在于数据库中。这就是为什么新数据不会出现在网格中的原因。