c# - 将 MySQL 与 Entity Framework 结合使用时遇到的问题

标签 c# .net mysql entity-framework connector-net

我在使用 Entity Framework 和 MySQL Connector/NET 6.4.4 插入行时遇到问题。当我尝试在添加 1 行后保存更改时,我得到一个带有以下堆栈跟踪的 System.NullReferenceException:

at MySql.Data.Entity.SqlGenerator.GenerateReturningSql(DbModificationCommandTree tree, DbExpression returning) 
at MySql.Data.Entity.UpdateGenerator.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.Entity.Internal.InternalContext.SaveChanges() 
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 
at System.Data.Entity.DbContext.SaveChanges() 
at UserData.Repository.DBRepositoryDataContext.SaveChanges() in C:\SoftwareProjects\Java\Degoo\Server\UserData\Repository\DBRepositoryDataContext.cs:line 42 
at UserData.Repository.Repository.<SaveChanges>b__1() in C:\SoftwareProjects\Java\Degoo\Server\UserData\Repository\Repository.cs:line 137 
at Microsoft.AppFabricCAT.Samples.Azure.TransientFaultHandling.RetryPolicy`1.ExecuteAction(Action action) in C:\SoftwareProjects\Java\Degoo\Server\Libraries\TransientFaultHandlingFramework\RetryPolicy.cs:line 352 

我使用“代码优先”的 EF 4.1。换句话说:我使用 DbSet 和 DbContext。

这就是我尝试添加的实体在 EF 的更改跟踪器中的样子:

     System.Data.Entity.Infrastructure.DbEntityEntry<Utilities.IStandardEntity> 
_internalEntityEntry    System.Data.Entity.Internal.InternalEntityEntry 
CurrentValues       System.Data.Entity.Infrastructure.DbPropertyValues 
Entity      UserData.PerformanceCounterEntities.PerformanceCounterEntity 
CallingClassName    "com.degoo.tests.integration.PerformanceCounterUploaderTest"    string 
Category    {System.Data.Entity.DynamicProxies.TextEntity_27BF3732ED645CE792F48B4AA75BFD05C10C0AE9C5B35779176C02A53A587F1A} UserData.PerformanceCounterEntities.TextEntity {System.Data.Entity.DynamicProxies.TextEntity_27BF3732ED645CE792F48B4AA75BFD05C10C0AE9C5B35779176C02A53A587F1A} 
CategoryTextID  1   long 
CreationTime    {2011-11-09 09:32:12}   System.DateTime 
Description {System.Data.Entity.DynamicProxies.TextEntity_27BF3732ED645CE792F48B4AA75BFD05C10C0AE9C5B35779176C02A53A587F1A} UserData.PerformanceCounterEntities.TextEntity {System.Data.Entity.DynamicProxies.TextEntity_27BF3732ED645CE792F48B4AA75BFD05C10C0AE9C5B35779176C02A53A587F1A} 
DescriptionTextID   3   long 
ID  0   long 
ModificationTime    {2011-11-09 09:32:12}   System.DateTime 
Name    {System.Data.Entity.DynamicProxies.TextEntity_27BF3732ED645CE792F48B4AA75BFD05C10C0AE9C5B35779176C02A53A587F1A} UserData.PerformanceCounterEntities.TextEntity {System.Data.Entity.DynamicProxies.TextEntity_27BF3732ED645CE792F48B4AA75BFD05C10C0AE9C5B35779176C02A53A587F1A} 
NameTextID  2   long 
NodeID  16485   long 
NumberOfSamples 1   long 
RowVersion  0   long 
Value   1.0 double

对于造成这种情况的原因有什么想法吗?它看起来像是 MySQL 的 EF 驱动程序中的一个错误(至少它们应该更新以提供更好的错误消息)。

最佳答案

我使用相同的连接器驱动程序时遇到了类似的问题。就我而言,其中一个对象使用属性而不是属性。

public class User : ProjectBasicEntity
{
   public string SomeColumn;
}

而不是

public class User : ProjectBasicEntity
{
   public string SomeColumn { get; set;}
}

我想有人真的需要改进这些驱动程序中的错误处理......

关于c# - 将 MySQL 与 Entity Framework 结合使用时遇到的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8063109/

相关文章:

java - Hibernate 将 Java 日期转换为 MySQL YEAR

mysql - 如何根据具有相同列/字段名称的表的联接结果轻松创建表

c# - 静态资源图像在运行时不出现(在设计时出现)

c# - 我想在后台运行程序。首选 C#

.net - 如何在运行时转换类型?

.Net/CLR 标识符

mysql - 坚持 ruby​​ 和 mysql

c# - 随着时间的推移造成的伤害统一

c# - Json.NET - 反序列化接口(interface)属性引发错误 "type is an interface or abstract class and cannot be instantiated"

c# - Jay,单声道 C# 编译器和更改语法