【问题标题】:An unhandled exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dllEntityFramework.SqlServer.dll 中出现“System.Data.Entity.Core.EntityCommandExecutionException”类型的未处理异常
【发布时间】:2016-06-29 18:38:31
【问题描述】:

我尝试了 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; }
        }
    }

请帮帮我!...

【问题讨论】:

  • “查看内部异常了解详情。” 内部异常说....?
  • 能否请您添加内部异常详细信息,因为查看您的代码,很难分辨出什么是破坏。但是您可以尝试一些事情,检查是否是 datetime2 转换错误,因为您正在插入日期,检查您是否将所需的值作为 null 传递。
  • 它没有任何内在!

标签: c# .net sql-server entity-framework


【解决方案1】:

检查模型类和实际数据库表之间的列名或外键映射名称是否不匹配。这通常是导致此错误的原因。

【讨论】:

    【解决方案2】:

    更新您的模型。上下文应该与数据库同步

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 2021-01-14
      • 2014-06-03
      相关资源
      最近更新 更多