我尝试了3天使用entityframework向我的数据库添加一行,但我不能:| ..现在我将所有必需品都用于向数据库添加行...最后我得到了这个错误:
EntityFramework.SqlServer.dll中发生未处理的“System.Data.Entity.Core.EntityCommandExecutionException”类型异常 - 附加信息:执行命令定义时发生错误。有关详细信息,请参阅内部异常。
我执行了几次数据库但是......!
代码:
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.ToString()),
date = dateTimePicker1.Value,
deadline = dateTimePicker2.Value,
buyername = bbuyerTextBox.Text.ToString(),
count = int.Parse(bcountTextBox.Text.ToString()),
profit = int.Parse(bprofitTextBox.Text.ToString())
};
database.buytbls.Add(tbl);
dataGridView1.DataSource = database.buytbls.ToList();
database.SaveChanges();
}
}
}
和表格:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WindowsFormsApplication6
{
using System;
using System.Collections.Generic;
public partial class buytbl
{
public int id { get; set; }
public string name { get; set; }
public int price { get; set; }
public int count { get; set; }
public int profit { get; set; }
public System.DateTime date { get; set; }
public System.DateTime deadline { get; set; }
public string buyername { get; set; }
public string description { get; set; }
}
}
请帮助我!...&lt; 3
答案 0 :(得分:1)
检查模型类和实际数据库表之间的列名称或外键映射名称是否存在不匹配。这通常是导致此错误的原因。
答案 1 :(得分:-1)
更新您的模型。上下文应与数据库同步