2016-06-28 110 views
0

我搜索了很多關於我的問題,但我沒有找到任何東西!實體框架不添加/保存到數據庫

我創建了一個數據庫和一個表,我的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=&quot;data source=SMQ2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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=&quot;data source=smq2;initial catalog=notebook;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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

有沒有錯誤?你是先遵循代碼還是數據庫優先的方法? – Prathyush

+0

我忘了我是什麼第一...我沒有得到任何錯誤 – Mohammadreza

+0

我現在得到這個:EntityFramework.dll中發生未處理的異常類型'System.InvalidOperationException' 其他信息:'price'屬性on 'buytbl'無法設置爲'System.Double'值。您必須將此屬性設置爲類型爲'System.Int32'的非空值' – Mohammadreza

回答

0

當你

database.buytbls.Add(tbl); 

您添加一個實體到數據庫的上下文

但承諾讓您的實體,您需要做的

database.SaveChanges(); 
+0

我在分部類中有SaveChanges – Mohammadreza

0

與您的問題沒有直接關係,但非常重要:您應該不能在多個操作上重用數據庫上下文在button1_Click內部,創建新的數據庫上下文實例,向其中添加一個對象,然後調用SaveChanges。然後,數據會出現。

現在答案。 SaveChanges必須在這兩行之間調用:

database.buytbls.Add(tbl); 
database.SaveChanges(); // <---- This line is missing 
dataGridView1.DataSource = database.buytbls.ToList(); 

數據不顯示的原因與DbContext的工作方式有關。最後一行,設置DataSource不會獲取DbContext中包含的數據,而是在數據庫上運行另一個查詢。而且,由於未調用SaveChanges,所以數據尚未存在於數據庫中。這就是爲什麼新數據不會出現在網格中。