gpt4 book ai didi

mysql - Entity Framework 5 + MySQL.Data 6.6.4.0 在数据插入时导致 NullReferenceException

转载 作者:行者123 更新时间:2023-11-29 01:30:27 25 4
gpt4 key购买 nike

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

  • 我可以通过创建命令直接插入数据,但是当我使用 Entity Framework 时,它会崩溃。
  • 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()

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;
}
}

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`)
);

最佳答案

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 )。但是,如果您这样做,如果您超过了最大值,您的数据库可能会返回无效值。

因此,更简单、更正确的解决方法是不使用无符号列或将现有列从无符号列更改为无符号列。某些 MySQL 设计器工具默认为未签名的 id 列,所以要小心!

关于mysql - Entity Framework 5 + MySQL.Data 6.6.4.0 在数据插入时导致 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15276448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com