实体框架:调用保存更改时出错

时间:2021-05-02 10:06:09

I am working with entity framework to connect MySql. I have created a entity data modal with the MySql database, it generate the connection string in web.config automatically which is as follows in my case:-

我正在使用实体框架来连接MySql。我用MySql数据库创建了一个实体数据模块,它自动在web.config中生成连接字符串,在我的例子中如下: -

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

Now I want to save record in database and the code is as follows:-

现在我想在数据库中保存记录,代码如下: -

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

        using (entityframework context =
              new entityframework())
        {

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

it gives error

它给出了错误

"object reference not set to an instance of object" on the if condition having "context.SaveChanges".

在具有“context.SaveChanges”的if条件下,“对象引用未设置为对象的实例”。

I also tried to pass the connection string at the time of creating context

我还尝试在创建上下文时传递连接字符串

string connectionstring = "SERVER=localhost;DATABASE= entityframework ;UID= root;";

but it gives error that "Keyword not supported: '"server'." this is my first effort in entity framework, is there anybody to advise me.

但它给出的错误是“关键字不支持:'”服务器'。“这是我在实体框架中的第一次努力,是否有人建议我。

2 个解决方案

#1


0  

If you can retrieve data from the server then the connection string is good.

如果您可以从服务器检索数据,那么连接字符串就是好的。

As for the SaveChanges() try calling context.ChangeTracker.DetectChanges() first and as a success condition you can set the initial id of the entity to -1 for instance and afterwards check if it is >= 0. (That is, if your PrimaryKey is also Autoincrement/Identity)

至于SaveChanges()首先尝试调用context.ChangeTracker.DetectChanges(),并且作为成功条件,您可以将实体的初始id设置为-1,然后检查它是否> = 0.(即,如果你的PrimaryKey也是自动增量/身份)

#2


0  

You need to also add the following to your web.config file (replace the Version string with the appropriate version of your MySQL .NET connector)

您还需要将以下内容添加到web.config文件中(将Version字符串替换为相应版本的MySQL .NET连接器)

<system.data><DbProviderFactories>
  <clear />
  <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.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories></system.data>

#1


0  

If you can retrieve data from the server then the connection string is good.

如果您可以从服务器检索数据,那么连接字符串就是好的。

As for the SaveChanges() try calling context.ChangeTracker.DetectChanges() first and as a success condition you can set the initial id of the entity to -1 for instance and afterwards check if it is >= 0. (That is, if your PrimaryKey is also Autoincrement/Identity)

至于SaveChanges()首先尝试调用context.ChangeTracker.DetectChanges(),并且作为成功条件,您可以将实体的初始id设置为-1,然后检查它是否> = 0.(即,如果你的PrimaryKey也是自动增量/身份)

#2


0  

You need to also add the following to your web.config file (replace the Version string with the appropriate version of your MySQL .NET connector)

您还需要将以下内容添加到web.config文件中(将Version字符串替换为相应版本的MySQL .NET连接器)

<system.data><DbProviderFactories>
  <clear />
  <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.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories></system.data>