微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EFCore 3的保存日期导致System.InvalidCastException

如何解决EFCore 3的保存日期导致System.InvalidCastException

我将EFCore 3.1与Code-First一起使用。 我的模特:

    [required]
    [Column(TypeName = "date")]
    public DateTime MyDate{ get; set; }

使用添加迁移时,这会在我的表中产生date,not null。 就像我想要的那样。

保存某些值(MyModel.MyDate = otherDateTime.Date.)时出现此错误

system.invalidCastException
Unable to cast object of type 'System.Data.sqlClient.sqlParameter' to type 'Microsoft.Data.sqlClient.sqlParameter'.
   at Microsoft.EntityFrameworkCore.sqlServer.Storage.Internal.sqlServerDateTimeTypeMapping.ConfigureParameter(DbParameter parameter)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.CreateParameter(DbCommand command,String name,Object value,Nullable`1 nullable)
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedRelationalParameter.AddDbParameter(DbCommand command,Object value)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command,IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject,Guid commandId,DbCommandMethod commandMethod)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject,CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection,CancellationToken cancellationToken)

当我从模型中删除[Column(TypeName = "date")]并且不进行新的迁移时,它正在工作。 但是,当我进行新迁移时,该列将更改为datetime:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<DateTime>(
        name: "MyDate",table: "MyTable",nullable: false,oldClrType: typeof(DateTime),oldType: "date");
}

似乎我缺少了一些东西。该如何解决

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。