我正在尝试将一个asp.net核心项目从2.2迁移到3.1,所以我不得不从EFCore2.2升级到EFCore3.1。
我试图直接升级2.2项目,但失败了,并创建了一个新项目,复制到了我的模型和DbContext上,现在在任何使用SQL中的序列来生成值的模型上调用MyDbContext.Add()时,都会得到一个“可空对象必须有值”错误。
这个错误不会发生在任何不使用序列的模型上,我可以调用add对那些非常好的模型。
--我正在尝试使用的模型--看起来像这样:
(请注意,我已重命名了每个属性,并删除了多次使用的数据类型)
public class MyModel
{
public int Id { get; set; }
private DateTime? timeStamp;
public DateTime TimeStamp
{
get { return timeStamp ?? DateTime.UtcNow; }
set { timeStamp = value; }
}
public int MySequence { get; private set; }
public string MySequenceWithPrefix
{
get
{
return "Prefix" + MySequence.ToString()
}
}
public MyEnumType EnumType { get; set; }
public string StringValue { get; set; }
public bool? NullableBoolean { get; set; }
public bool NotNullableBoolean { get; set; }
public int? NullableFK { get; set; } //For the below navigation property
public MyNavigationProperty NavigationProperty { get; set; }
public ICollection<MyCollectionOfThings> Things { get; set; }
public DateTime? NullableDateTime { get; set; }
public MyNullableEnumProperty? NullableEnumProperty { get; set; }
[DataType(DataType.Currency)] public double? NullableCurrencyField { get; set; }
}
I将模型构建器中的序列配置为:
modelBuilder.HasSequence<int>("MySequence")
.StartsAt(40000)
.IncrementsBy(1);
modelBuilder.Entity<MyModel>()
.Property(model => model.MySequence)
.HasDefaultValueSql("NEXT VALUE FOR MySequence");
我试图像这样调用MyDbContext.Add():
MyModel testMyModel = new MyModel()
{
//set non-nullable fields here
};
//throws "Nullable object must have a value."
MyDbContext.Add(testMyModel);
我不确定是否有一个突破性的变化,从EF核心2.2到3.1涉及序列,或我是否设置了一些不正确的时候,我的新项目和移动模型手动。
如果您需要进一步的信息,请告诉我。
编辑:具有堆栈跟踪的完整错误消息如下:
InvalidOperationException: Nullable object must have a value.
System.Nullable<T>.get_Value()
lambda_method(Closure , InternalEntityEntry )
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry+OriginalValues..ctor(InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.EnsureOriginalValues()
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntrySubscriber.SnapshotAndSubscribe(InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, bool acceptChanges, bool modifyProperties)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, bool acceptChanges, bool modifyProperties, Nullable<EntityState> forceStateWhenUnknownKey)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode<ValueTuple<EntityState, EntityState, bool>> node)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph<TState>(EntityEntryGraphNode<TState> node, Func<EntityEntryGraphNode<TState>, bool> handleNode)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, bool forceStateWhenUnknownKey)
Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
Microsoft.EntityFrameworkCore.DbContext.SetEntityState<TEntity>(TEntity entity, EntityState entityState)
Microsoft.EntityFrameworkCore.DbContext.Add<TEntity>(TEntity entity)
MyProject.Areas.MyArea.Controllers.MyController.Form(FormValues values) in MyController.cs
MyDbContext.Add(testMyModel);
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
如果堆栈跟踪太长,请提前道歉。我不知道你到底需要什么信息。
https://stackoverflow.com/questions/61739035
复制相似问题