[英]Always Encrypted SQL 2016 and Entity Framework 6: “Operand type clash: datetime2 is incompatible with date”
Current project: 当前的项目:
MapToStoredProcedures();
MapToStoredProcedures();
in EntityTypeConfiguration
EntityTypeConfiguration
Unlike the repository pattern tutorial, I was able to push my model to DB. 与存储库模式教程不同,我能够将模型推送到数据库。 I am not working off of a hand-assembled DB.
我不是在手工组装数据库上工作。 I did modify the migration as per the two SQL statements in second Always Encrypted link, not surprising also for a birthdate and a Social Security Number.
我确实按照第二个“始终加密”链接中的两个SQL语句修改了迁移,对于生日和社会保险号也不足为奇。
Because my Entity Framework entities specifies the encrypted fields as such, 由于我的实体框架实体是这样指定加密字段的,
Property(x => x.Dob).HasColumnOrder(15).HasColumnName("Dob").HasColumnType("Date").IsRequired();
Property(x => x.Sin).HasColumnOrder(16).HasColumnName("Sin").HasColumnType("nvarchar").HasMaxLength(11).IsRequired();
I have decorated those two fields in my User
domain entity in the following way: 我已经通过以下方式在
User
域实体中修饰了这两个字段:
[DataType(DataType.Date)]
public DateTime Dob { get; set; }
[MaxLength(11)]
public string Sin { get; set; }
as per this comment . 根据这个评论 。 As a precaution, I have also decorated the same fields in my
IdentityUser.cs
file. 作为预防措施,我还修饰了
IdentityUser.cs
文件中的相同字段。
When I try to push the default administrative user I am trying to create, using this: 当我尝试推送默认的管理用户时,我尝试使用以下方法:
public async Task<ActionResult> AddAdmin() {
var user = _userManager.FindByName("email@domain.net");
if(user == null) {
user = new IdentityUser() {
// Other fields
Dob = new DateTime(1972, 10, 12),
Sin = "726-261-050",
// Other fields
};
var createUser = await _userManager.CreateAsync(user, "password");
if (createUser.Succeeded) {
var userId = _userManager.FindByName("email@domain.net").Id;
var addRole = await _userManager.AddToRoleAsync(userId, "Admin");
if (addRole.Succeeded) {
return View("Index");
}
}
}
}
I get the following error: 我收到以下错误:
Operand type clash: datetime2(7) encrypted with (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK1', column_encryption_key_database_name = 'database') is incompatible with date encrypted with (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK1', column_encryption_key_database_name = 'database')
操作数类型冲突:使用(encryption_type ='RANDOMIZED',encryption_algorithm_name ='AEAD_AES_256_CBC_HMAC_SHA_256',column_encryption_key_name ='CEK1',column_encryption_key_database_name ='database'类型加密的datetime2(7)与( 'AEAD_AES_256_CBC_HMAC_SHA_256',column_encryption_key_name ='CEK1',column_encryption_key_database_name ='数据库')
which occurs at the _userManager.CreateAsync()
. 发生在
_userManager.CreateAsync()
。
I am completely at a loss as to why this is happening. 我完全不知道为什么会这样。 I have clearly specified
Dob
in IdentityUser()
as a Date
field, and not DateTime2
. 我已经在
IdentityUser()
中将Dob
明确指定为Date
字段,而不是DateTime2
。 The same goes for the raw User
model/domain and its associated EF entity. 原始
User
模型/域及其关联的EF实体也是如此。 The actual DB field is a Date
field (confirmed this personally), so I can see why the destination is a problem if the source is mysteriously created as a DateTime2
, even though the IdentityUser()
Dob
field is a Date
as well. 实际的DB字段是一个
Date
字段(对此进行了个人确认),因此,即使IdentityUser()
Dob
字段也是一个Date
,我也可以看到如果将源神秘地创建为DateTime2
,那么为什么目的地是一个问题。 Theoretically, a Date
field in IdentityUser()
cannot pass on a DateTime2
to the DB. 从理论上讲,
IdentityUser()
的Date
字段不能将DateTime2
传递给DB。
I believe EF handles all the date types as datetime2. 我相信EF将所有日期类型都作为datetime2处理。 Changing the column type in SQL server to datetime2 should help.
将SQL Server中的列类型更改为datetime2应该会有所帮助。 As of now, your only option is to change the datatype of your column to datetime2 in SQL Server, since EF does not have any hooks into parameter generation.
到目前为止,您唯一的选择是在SQL Server中将列的数据类型更改为datetime2,因为EF在参数生成方面没有任何钩子。
If we don't want to change the datatype of from datetime
to datetime2
. 如果我们不想将数据类型从
datetime
更改为datetime2
。 because datetime to extend the 7 digit after last decimal. 因为datetime扩展了最后一个小数点后的7位数字。
check by below script. 通过以下脚本检查。 difference b/w datetime and datetime2.
黑白日期时间和日期时间的差异2。
select * from newdatetimetest ALTER TABLE newdatetimetest ALTER column mydatetime datetime2
select * from newdatetimetest ALTER TABLE newdatetimetest ALTER column mydatetime datetime
select * from newdatetimetest
You can implement the custom interceptor to change the entityframework datetime2 datatype convert into datetime. 您可以实现自定义拦截器,以将entityframework datetime2数据类型转换为datetime。
we have successfully implemented and insert the record successfully. 我们已成功实施并成功插入了记录。
find the reference form below link. 在下面的链接中找到参考表格。
https://github.com/aspnet/EntityFramework6/issues/578 https://github.com/aspnet/EntityFramework6/issues/578
https://github.com/aspnet/EntityFramework6/pull/1147 https://github.com/aspnet/EntityFramework6/pull/1147
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.