开发者

Entity Framework and MySQL null reference exceptions when inserting a row

开发者 https://www.devze.com 2023-01-29 14:24 出处:网络
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 def

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.开发者_JAVA技巧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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消