简体   繁体   English

始终加密的SQL 2016和Entity Framework 6:“操作符类型冲突:datetime2与日期不兼容”

[英]Always Encrypted SQL 2016 and Entity Framework 6: “Operand type clash: datetime2 is incompatible with date”

Current project: 当前的项目:

  • MVC 5.2 MVC 5.2
  • DotNet 4.6.2 点网4.6.2
  • EF 6 EF 6
  • Identity 2 (modified as per the repository pattern below) 身份2(根据下面的存储库模式修改)
  • SQL Server 2016 RTM SQL Server 2016 RTM
  • Repository Pattern: Persistence-Ignorant ASP.NET Identity 存储库模式: 无需持久性的ASP.NET身份
  • Always Encrypted column encryption as described here and here 如此此处所述,始终加密列加密
  • All SQL as MapToStoredProcedures(); 所有SQL都为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.

相关问题 操作数类型冲突:datetime2 与 tinyint 不兼容 - Operand type clash: datetime2 is incompatible with tinyint 操作数类型冲突:datetime2与int不兼容(介于两者之间) - Operand type clash: datetime2 is incompatible with int (Between) FF4j + MS SQL:获取“操作数类型冲突 datetime2 与时间戳不兼容”异常 - FF4j + MS SQL : Getting "operand type clash datetime2 is incompatible with timestamp" Exception SQL Server始终加密的操作数类型冲突:运行EXEC sproc时,varchar与varchar(60)不兼容 - SQL Server Always Encrypted Operand type clash: varchar is incompatible with varchar(60) when running EXEC sproc SQL Server始终加密:操作数类型冲突:varchar与varchar(max)不兼容 - SQL Server Always Encrypted: Operand type clash: varchar is incompatible with varchar(max) System.Data.SqlClient.SqlException: &#39;操作数类型冲突:datetime2 与十进制不兼容 - System.Data.SqlClient.SqlException: 'Operand type clash: datetime2 is incompatible with decimal SQL Server 2012(触发器)操作数类型冲突:int与日期不兼容 - SQL Server 2012 (triggers) Operand type clash: int is incompatible with date SQL错误IS操作数类型冲突:int与日期不兼容 - SQL ERROR IS Operand type clash: int is incompatible with date SQL Server Operand类型冲突:日期与int不兼容 - Sql Server Operand type clash: date is incompatible with int MS SQL Operand类型冲突:日期与bigint不兼容 - MS SQL Operand type clash: date is incompatible with bigint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM