我搜索了很多關於我的問題,但我沒有找到任何東西!實體框架不添加/保存到數據庫
我創建了一個數據庫和一個表,我的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>
謝謝你們!...
有沒有錯誤?你是先遵循代碼還是數據庫優先的方法? – Prathyush
我忘了我是什麼第一...我沒有得到任何錯誤 – Mohammadreza
我現在得到這個:EntityFramework.dll中發生未處理的異常類型'System.InvalidOperationException' 其他信息:'price'屬性on 'buytbl'無法設置爲'System.Double'值。您必須將此屬性設置爲類型爲'System.Int32'的非空值' – Mohammadreza