Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Always Encrypted] EF Core 6.0 Operand type clash : decimal(29, 27) is incompatible with money encrypted #27831

Closed
Unnamed1984 opened this issue Apr 18, 2022 · 3 comments · Fixed by #29051
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@Unnamed1984
Copy link

Column X in a database is encrypted with Always Encrypted keys and has type money.
EF entity has a property X with type decimal and the following configuration:
modelBuilder.Property(p => p.X).HasColumnType("money");

The exception I get while trying to insert the entity and call SaveChanges:
image

@roji
Copy link
Member

roji commented Apr 18, 2022

@Unnamed1984 can you please submit a minimal, runnable code sample showing how the above exception happens? If you configure your property as money (as above), EF should be generating decimal(29,27).

@Unnamed1984
Copy link
Author

Unnamed1984 commented Apr 22, 2022

@roji

  1. Generate a column master key with SSMS as described here:
    https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/configure-always-encrypted-keys-using-ssms?view=sql-server-ver15

  2. Run the following scripts on your database:

CREATE COLUMN ENCRYPTION KEY [Column_Encryption_Key_1] WITH VALUES ( COLUMN_MASTER_KEY = [Column_Master_Key_1], ALGORITHM = 'RSA_OAEP', ENCRYPTED_VALUE = 0x01E6000001680074007400700073003A002F002F006B0076002D006E00670061002D0064002D006300750073002D00630061006C0063002D00300031002E007600610075006C0074002E0061007A007500720065002E006E00650074002F006B006500790073002F00630061006C0063002D0061006C0077006100790073002D0065006E0063007200790070007400650064002D006D00610073007400650072002D006B00650079002F003800630062003900650064006500320033006600310039003400320066003600390066006200300065006100630037003200360064003600370066003200390071CE461C0E03A2DE3516ADFE07893D10C84046091E068638DA16C164B17C3E053A78CDE66773025591B5CB0D9C1C245C51C565FC291179A02008BDCBBAC6A87F8327B93DA30AA465F82CD091AD2931787529878BFAB88EDB604F15AFD1E1E3661C5A9864FFF35EE51C1105A9A70E0D9D940D4D211A801D60CAAA60DB5690328F235C8F29DE660BC739CC7547660D0DB76D4301036E6C1554B8B370FF5DF15DF40C67DC046930DA0903F60D4C61C9DE87F1870BC840BBE6E256C682468769DF8A24720AC2EC712A321DE343C524D67887B53041424EE7ABA833056F1540A73195B9FB6F3B8513BEF782F0D62729B9F9C69A5FE5AB82511578771A3BE3F7C36E4D8C9E992A3C7A78C28CC8D35EC93D7C82BBDA9A8DB035AB7C85977CAF9DF8ADB7CA83950F5E3930A93A6B52FBF782DCB4EA802B5B70C4625F70E7620F9BEFDD39EAEE42B8D7B905B8D15A8969904FA653E51442391F0C6EBD213DC7E2A6B5F83EBFFDC689A2E7FA16C8E56DC3378A67E846B79248ACC67AD4406E4F7332FE8B54FDC453E59E4E4FF8E20013F655A554E474C4CE49E05628C9DD3138F0689CEBDA4CD88677984D2E988F431F01F51EEC9AE20505F10C06656BCFED25D921FCA8C46C45380517A47F197B7656DE670A6FC994198261D1BFB38132A0FB2AD86E6E8D2A1A884EDBF452F17C9288F64C21A5A08253BB8D0599158EAE09740B98E9F617 )

CREATE TABLE [dbo].[Payment] ( Id INT IDENTITY (1, 1) NOT NULL, Amount MONEY ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [Column_Encryption_Key_1], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL, CONSTRAINT [PK_PAYMENT] PRIMARY KEY CLUSTERED (Id));

  1. Build and run this application:
    csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
  </ItemGroup>

</Project>

Data.DataContext.cs:

using Microsoft.EntityFrameworkCore;

namespace EFEncryptionTest.Data
{
	public class DataContext : DbContext
	{
		public DataContext(DbContextOptions options) : base(options)
		{
		}

		public virtual DbSet<Payment> Payments { get; set; }

		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			modelBuilder.Entity<Payment>(x =>
				{
					x.ToTable("Payment");
					x.Property(x => x.Amount).HasColumnType("money");
				});
		}
	}
}

Data.Payment.cs:

namespace EFEncryptionTest.Data
{
	public class Payment
	{
		public int Id { get; set; }

		public decimal Amount { get; set; }
	}
}

Program.cs:

using EFEncryptionTest.Data;
using Microsoft.EntityFrameworkCore;

DbContextOptions options = new DbContextOptionsBuilder()
	.UseSqlServer("Server=(local);Database=t1;Integrated Security=true;Column Encryption Setting=enabled")
	.Options;
DataContext dataContext = new DataContext(options);

dataContext.Payments.Add(new Payment { Amount = 22.3m });
await dataContext.SaveChangesAsync();
  1. End up with the following issue:
    image

Precision and scale for some reason differ from what I mentioned before, maybe that's because in my current project I use a Column Master Key based on a Key Vault, but the main issue is the same.

@ajcvickers
Copy link
Member

Note for triage: parameter DbType is Decimal. Likely needs to be Currency and/or SqlDbType needs to be Money.

@ajcvickers ajcvickers self-assigned this Apr 26, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Apr 28, 2022
ajcvickers added a commit that referenced this issue Sep 11, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 11, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 12, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
3 participants