I implemented the Always Encrypted Concept with entity framework in my application through this below link.
This is the Up () migration method in my initialSchema.cs file under migrations folder of my current application.
public override void Up()
{
CreateTable(
"dbo.PersonalInfoTables",
c => new
{
ID = c.Int(nullable: false, identity: true),
Firstname= c.String(),
LastName= c.String(),
Address= c.String(),
SSN = c.String(),
})
.PrimaryKey(t => t.ID);
}
After that I comment the SSN filed and added the below SQL statement for encrypting the SSN column.
//manually add the encrypted columns
Sql("ALTER TABLE [dbo].[PersonalInfoTables] ADD [SSN] [nvarchar](11) COLLATE Latin1_General_BIN2 ENCRYPTED WITH(ENCRYPTION_TYPE = DETERMINISTIC, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = CEK_Auto1) NOT NULL");
And then run the Update-Database command in Package Manger Console.
The above code will work only if the CEK key already available for the encrypted column. but when ever I run my application once again after delete the existing local database, it gives the exception like CEK key is not available and also did not apply the migrations.
How can I resolve the above issue, after added ALTER SQL statement and delete the local database?
The above issue resolved only, changed Up () migration code once again, but I don’t want that scenario.