插入行时实体框架和 MySQL 空引用异常

发布于 2024-10-07 08:58:10 字数 3003 浏览 1 评论 0原文

我在 EDMX 文件中创建了一个简单的测试表。该表名为 Test,具有一列 TestId。 TestId是Entity Key,Type是Guid。 StoreGeneratePattern 设置为 Identity(默认值)。

生成的 SQL 如下所示:

CREATE TABLE `Tests` (
    `TestId` CHAR(36) BINARY  NOT NULL
);

ALTER TABLE `Tests`
ADD CONSTRAINT `PK_Tests`
    PRIMARY KEY (`TestId` );

现在我的代码正文如下所示:

        using (var foo = new TestModelContainer())
        {
            var test = new Test() {
                TestId = Guid.NewGuid()
            };

            foo.Tests.AddObject(test);
            foo.SaveChanges();
        }

当我 SaveChanges 时,我收到空引用异常。堆栈跟踪深入到 MySql 连接器的内部(我使用的是 6.3.5 版本):

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=MySql.Data.Entity
  StackTrace:
       at MySql.Data.Entity.SqlGenerator.GenerateReturningSql(DbModificationCommandTree tree, DbExpression returning)
       at MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
       at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
       at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
       at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
       at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
       at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
       at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
       at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at EFMySQL.Program.Main(String[] args) in C:\Users\david.pio\Documents\Visual Studio 2010\Projects\EFMySQL\EFMySQL\Program.cs:line 40
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

有什么想法吗?

I've created a simple Test table in an EDMX file. The table is called Test with one column, TestId. TestId is the Entity Key, and it's Type is Guid. StoreGeneratedPattern is set to Identity (the default).

The generated SQL looks like this:

CREATE TABLE `Tests` (
    `TestId` CHAR(36) BINARY  NOT NULL
);

ALTER TABLE `Tests`
ADD CONSTRAINT `PK_Tests`
    PRIMARY KEY (`TestId` );

now the body of my code looks like this:

        using (var foo = new TestModelContainer())
        {
            var test = new Test() {
                TestId = Guid.NewGuid()
            };

            foo.Tests.AddObject(test);
            foo.SaveChanges();
        }

I'm getting a null reference exception when I SaveChanges. The stack trace goes deep into the guts of the MySql connector (I'm using version 6.3.5):

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=MySql.Data.Entity
  StackTrace:
       at MySql.Data.Entity.SqlGenerator.GenerateReturningSql(DbModificationCommandTree tree, DbExpression returning)
       at MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
       at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
       at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
       at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
       at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
       at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
       at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
       at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at EFMySQL.Program.Main(String[] args) in C:\Users\david.pio\Documents\Visual Studio 2010\Projects\EFMySQL\EFMySQL\Program.cs:line 40
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-10-14 08:58:10

解决了我自己的问题

在 EDMX 设计器中有一个名为 StoreGeneratePattern 的属性。对于 EntityKey 设置为 Identity。由于我在应用程序代码中生成主键而不依赖于数据库,所以我知道我不想要这个...但是,当生成 DDL 脚本时,表创建脚本中没有任何内容可以强制执行身份,所以我想没关系。

当我将值从 Identity 更改为 None 时,它​​起作用了....去看看

Solved my own question

In the EDMX designer there is a property called StoreGeneratedPattern. For the EntityKey is is set to Identity. Since I am generating my primary keys in app code not relying on the DB, I know I don't want this...however when the DDL script gets generated, there is nothing in the table creation script that enforces the identity so I figured that was OK.

When I changed the value from Identity to None, it works....go figure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文