实体框架5 + MySQL.Data 6.6.4.0导致数据插入时出现NullReferenceException

fir*_*ude 2 mysql entity-framework-5

我正在使用MySQL的Entity Framework,每次我尝试插入数据时它都会给我一个NullReferenceException.

  • 我可以通过创建命令直接插入数据,但是当我使用Entity Framework时它就会爆炸.
  • 实体框架将从表或更新表中进行选择,因此这可能与主键有关

从SaveChanges()方法抛出以下异常.


failed: System.NullReferenceException : Object reference not set to an instance of an object.
    at MySql.Data.Entity.ListFragment.WriteSql(StringBuilder sql)
    at MySql.Data.Entity.SelectStatement.WriteSql(StringBuilder sql)
    at MySql.Data.Entity.InsertStatement.WriteSql(StringBuilder sql)
    at MySql.Data.Entity.SqlFragment.ToString()
    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()
Run Code Online (Sandbox Code Playgroud)

EF 5

public decimal CreateAlertNotification2(ulong alertServiceId, Alerting.alert_service_notification_type notificationType, string recipientName, string recipientEndpoint)
        {
            using (risk_fleetEntities dbContext = new risk_fleetEntities())
            {
                var newNotification = new alert_service_notification();
                newNotification.sys_alert_service_id = alertServiceId;
                newNotification.name = recipientName;
                newNotification.notification_type = Enum.GetName(typeof(Alerting.alert_service_notification_type), notificationType);
                newNotification.recipient = recipientEndpoint;
                dbContext.alert_service_notification.AddObject(newNotification);
                dbContext.SaveChanges();

                return newNotification.id;
            }
        }
Run Code Online (Sandbox Code Playgroud)

MySQL 5


CREATE TABLE `alert_service_notification` (
  `id` bigint(20) unsigned AUTO_INCREMENT PRIMARY KEY, 
  `sys_alert_service_id` bigint(20) unsigned NOT NULL,
  `notification_type` ENUM("SMS", "Email"),
  `name` CHAR(50),
  `recipient` CHAR(50),
  FOREIGN KEY (`sys_alert_service_id`) REFERENCES `alert_service`(`id`) 
);
Run Code Online (Sandbox Code Playgroud)

mat*_*ser 5

EF不支持无符号整数,因此在这种情况下,它必须将bigint存储为小数,因为Int64不够大(9,223,372,036,854,775,807最大值与无符号bigint的18,446,744,073,709,551,615).它也将存储unsigned int Int64而不是Int32出于同样的原因.

如果您尝试更改EF设计器中的类型,它并不能完全正确地执行所有操作,并在运行时抛出这样的错误.您实际上可以打开edmx并对其进行编辑以解决此问题,您可以欺骗EF以假设该列不是真正无符号的.只需查找列的所有引用,并确保它们都没有提及Int64/ decimal(取决于您是否需要Int32/ Int64).但是,如果您这样做,如果您超过最大值,您的数据库可能会返回无效值.

因此,更简单和正确的修复不是使用unsigned或更改unsigned中的现有列.某些MySQL设计器工具默认为unsigned id列,因此请注意!