今日新闻:朝鲜要改革开放了!!!比你牛逼的人都在努力,你还有理由懒惰吗?
宇宙强大IDE配套的EF问题记录
今天做数据添加时,Id我设置为int类型了,结果在做Add操作时报的错让我摸不着头脑,后来问了问大神最终还是处理掉了。
异常如下:
System.Data.Entity.Infrastructure.DbUpdateException
HResult=0x80131501
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at Keying.Repositories.UnitOfWork.Commit() in D:\WorkSpace\WMS\Keying.Repositories\UnitOfWork.cs:line 38
at Keying.DomainService.System.Service.SysMessageService.Add(SysMessageDTO sysMessageDTO) in D:\WorkSpace\WMS\Keying.DomainService\System\Service\SysMessageService.cs:line 42
at OptimusPrime.Client.Main.Views.MainView.SaveMsgToDB() in D:\WorkSpace\WMS\OptimusPrime.Client\OptimusPrime.Client.Main\Views\MainView.xaml.cs:line 125
at OptimusPrime.Client.Main.Views.MainView.<Timer_Tick>b__10_0() in D:\WorkSpace\WMS\OptimusPrime.Client\OptimusPrime.Client.Main\Views\MainView.xaml.cs:line 72
at System.Threading.Tasks.Task.Execute()
内部异常 1:
UpdateException: An error occurred while updating the entries. See the inner exception for details.
内部异常 2:
SqlException: 不能将值 NULL 插入列 'id',表 'KEY_NEWWMS.dbo.sysmessage';列不允许有 Null 值。INSERT 失败。
语句已终止。
检查几遍我的Entity属性Id都不为null,见鬼了???
最后得知EF中int类型为默认自增,我们需要手动标记为:[DatabaseGenerated(DatabaseGeneratedOption.None)]。
在使用Entity Framework进行数据添加操作时,遇到因未正确标记int类型的ID字段而导致的异常。本文详细记录了解决过程,强调了在EF中使用int作为ID时,需明确指定其非自增属性。
964

被折叠的 条评论
为什么被折叠?



