插入行时实体框架和 MySQL 空引用异常
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了我自己的问题
在 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