简体   繁体   English

MySQL +实体框架中的对象刷新错误

[英]Object Refrence Error in MySQL + Entity framework

I am trying to learn entity framework with MySql database. 我正在尝试使用MySql数据库学习实体框架。
Trying to save the data with following code: 尝试使用以下代码保存数据:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (txtName.Text == string.Empty ||
    txtAge.Text == string.Empty)
    {
        lblMsg.Text = "Please enter proper details first";
        return;
    }

    employee emp = new employee();
    emp.Name = txtName.Text;
    emp.Age = Convert.ToInt32(txtAge.Text);

    using (entityframeworkEntities context =
    new entityframeworkEntities())
    {

        context.AddToemployees(emp);
        if (context.SaveChanges() == 1)  // Error Part
        {
            lblMsg.Text = "Saved Successfully.";
        }
    }
}

The code is getting an Object Reference error on if (context.SaveChanges() == 1) . 代码在if (context.SaveChanges() == 1)上收到Object Reference错误。 Even the emp is not added to the contaxt object. 甚至emp也不会添加到contaxt对象中。

When I debug the code, the debugger goes to following part: 当我调试代码时,调试器转到以下部分:

public entityframeworkEntities() :                               
base("name=entityframeworkEntities", "entityframeworkEntities") 
{
    this.OnContextCreated();   
}

Even on this part, the debugger comes to the braces but skip this.OnContextCreated(); 即使在这部分上,调试器也要花括号,但请跳过此this.OnContextCreated(); line. 线。

Here is my connection string in Web.Config 这是我在Web.Config中的连接字符串

<add name="entityframeworkEntities" connectionString="metadata=res://*/ApplicationWithMySql.csdl|res://*/ApplicationWithMySql.ssdl|res://*/ApplicationWithMySql.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=root;password=root;database=entityframework&quot;" providerName="System.Data.EntityClient" />

Please help me, where am I going wrong? 请帮助我,我要去哪里了?

Thank you! 谢谢!

Just a couple of things u might have overlooked. 您可能忽略了几件事。 not so much an answer. 与其说答案,不如说是。 quite long to post as a comment 很长才能发表评论

<system.data>
    <DbProviderFactories>
        <remove invariant="MySql.Data.MySqlClient" />
        <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

a.build clean. a。建造干净。

b.build mysql dll into ur bin directory b。将mysql dll编译到ur bin目录中

c.Install nuget packages for MySQL.Data and MySQL.Data.Entities.((right click on assembly and ensure Copy Local is set to true before building) c。为MySQL.Data和MySQL.Data.Entities安装nuget程序包((右键单击程序集并确保在构建之前将Copy Local设置为true)

d.Remove Version=6.3.5.0 tag entirely and build without any versioning on VS.type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" instead of specifying the full version data. d。完全删除Version = 6.3.5.0标记,并在VS.type =“ MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data”上构建任何版本而无需指定任何版本,而不是指定完整版本数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM