我首先使用带有代码的efcore3.1。
我的型号:
[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");
}
我好像错过了什么。如何解决这个问题?