我正在使用MySQL的Entity Framework,每次我尝试插入数据时它都会给我一个NullReferenceException。
从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`)
);
答案 0 :(得分: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设计器工具默认为无符号id列,因此请注意!
答案 1 :(得分:0)
我的解决方案非常简单。刚编辑数据库没有“无符号”变量,并从中构建了模型。像魅力一样工作,不需要手动编辑。此外,在生成模型后将它们更改回无符号似乎没有问题。 :)