0

我正在尝试使用 MySql 数据库学习实体框架。
尝试使用以下代码保存数据:

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.";
        }
    }
}

代码Object Referenceif (context.SaveChanges() == 1). 甚至emp没有添加到contaxt对象中。

当我调试代码时,调试器会转到以下部分:

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

即使在这部分,调试器也来到了大括号,但跳过了this.OnContextCreated();行。

这是我在 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" />

请帮助我,我哪里出错了?

谢谢!

4

1 回答 1

0

只是你可能忽略了几件事。没有那么多答案。很长时间才能发表评论

<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.构建干净。

b.build mysql dll到你的bin目录

c.为 MySQL.Data 和 MySQL.Data.Entities 安装 nuget 包。((右键单击程序集并确保在构建之前将 Copy Local 设置为 true)

d. 完全删除 Version=6.3.5.0 标记并在 VS.type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" 上构建没有任何版本控制,而不是指定完整的版本数据。

于 2013-01-30T10:47:39.017 回答