Entity Framework:在 MySQL Table 中插入行时出现异常

Entity Framework: Getting exception while inserting row in MySQL Table

我最近将我的数据库从 SQL 服务器切换到 MySQL。我还在使用 Entity Framework 6 在数据库中进行 CRUD 操作。当我使用 SQL 服务器作为后端时,我将“StoreGeneratedPattern”设置为 Table 的主列的“身份”,以便它可以生成唯一的 GUID。我在 MySQL 中采用相同的方法,但收到以下错误。唯一的区别是 SQL 服务器中主键的数据类型是 GUID,而在 MySQL 中是 varchar,默认为 UUID()。你能帮忙找出问题所在吗?

每当我将“StoreGeneratedPattern”设置为None并从代码中为主键指定一个任意值时,它就可以正常工作。但是我不想从代码生成主键值。

Object Reference not set to an instance.

MySQL Table

CREATE TABLE Gender
(
    org_genderid VARCHAR(40) NOT NULL PRIMARY KEY DEFAULT(uuid()),  
    org_gendername varchar(100) NOT NULL,   
    org_ispublished bit NOT NULL,       
    org_createdby varchar(40) NOT NULL,
    org_createdon datetime NOT NULL,
    org_modifiedby varchar(40) NOT NULL,
    org_modifiedon datetime NOT NULL
);

Entity Framework插入数据

请注意这里使用的每个属性都包含值

using (var ctxAddGender = new STREAM_EMPLOYEEDBEntities())
            {
                var entGender = new gender()
                {
                    org_gendername = vmGender.Name,
                    org_ispublished = true,
                    org_createdby = vmGender.CreatedBy,
                    org_createdon = DateTime.Now.ToUniversalTime(),
                    org_modifiedby = vmGender.ModifiedBy,
                    org_modifiedon = DateTime.Now.ToUniversalTime(),

                };

                ctxAddGender.genders.Add(entGender);

                int success = ctxAddGender.SaveChanges();
}

标识为 StoreGeneratedPattern

堆栈跟踪

   at MySql.Data.EntityFramework.ListFragment.WriteSql(StringBuilder sql)
   at MySql.Data.EntityFramework.SelectStatement.WriteSql(StringBuilder sql)
   at MySql.Data.EntityFramework.InsertStatement.WriteSql(StringBuilder sql)
   at MySql.Data.EntityFramework.SqlFragment.ToString()
   at MySql.Data.EntityFramework.InsertGenerator.GenerateSQL(DbCommandTree tree)
   at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(Dictionary`2 identifierValues)
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<>c.<Update>b__21_0(UpdateTranslator ut)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
   at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__153_0()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass148_0.<SaveChangesInternal>b__0()
   at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at Application.Business.BLL.BusinessLayer.GenderBLL.AddGender(GenderViewModel vmGender) in G:\onMyTune\IP\MySQL Projects\Project Acceleration - Employee Web API\Application.Business\BLL\BusinessLayer\GenderBLL.cs:line 33
   at Application.WebAPI.Controllers.GenderController.AddGender(GenderViewModel vmGender) in G:\onMyTune\IP\MySQL Projects\Project Acceleration - Employee Web API\Project Acceleration - Employee Web API\Controllers\GenderController.cs:line 36

尝试以下一些方法:

  • 如果 table 为空,则使用 ALTER TABLE 性别 AUTO_INCREMENT = 0 重置 AUTO_INCREMENT。
  • 您可以重新创建 table。
  • 从您的项目中删除映射的实体,然后从数据库中re-import删除它。

问题可能是 EF 在尝试执行 update/insert 之前正在验证您的主键是否为 NULL。如果是这样的话;

在您的实体 class 中将 genderid 值设置为 Guid.NewGuid().ToString()