I have am Emp table that looks like this:
.
I have created a stored procedure which looks like this
ALTER PROCEDURE [dbo].[GetAllEmployees]
AS
BEGIN
OPEN SYMMETRIC KEY TestTableKey DECRYPTION
BY CERTIFICATE EncryptTestCert
SELECT
EMPId,
Firstname,
Lastname,
AddessId,
JobId,
DateofBirth,
CONVERT(NVARCHAR(50),DECRYPTBYKEY(EncryptFirstname)) AS [EncryptFirstname],
CONVERT(NVARCHAR(50),DECRYPTBYKEY(EncryptLastname)) AS [EncryptLastname]
FROM
EMPloyee
END
The reason I have created a stored procedure is to convert some of the varbinary columns in the table to strings and use it with EF in MVC3.
The way I have mapped the procedure in the model is as below.


In my EmployeeViewModel this is the way I am mapped the properties to fields
[Description("EmployeeDetails")]
public class EmployeeViewModel: IEmployeeModel
{
public Guid EmpId { get; set; }
[Display(Name = "EncryptLastName")]
public byte[] EncryptLastName { get; set; }
[Display(Name = "EncryptFirstName")]
public byte[] EncryptFirstName { get; set; }
[Display(Name = "LastName")]
public string LastName { get; set; }
}
In access the stored procedure in my service method, this is the way I am accessing it.
public List<EmployeeViewModel> GetEmpList()
{
var ent = new EncryptionEntities();
List<Employee> allEmp = new List<Employee>();
allEmp = ent.GetEmployees().ToList();
ConvertViewModelObject cvmo = new ConvertViewModelObject();
List<EmployeeViewModel> empVM = new List<EmployeeViewModel>();
foreach (var item in allEmp)
{
empVM.Add(cvmo.ConvertFromEmployee(item));
}
return empVM.ToList();
}
ERROR in this method:
The 'EncryptFirstname' property on 'Employee' could not be set to a 'String' value. You must set this property to a non-null value of type 'Byte[]'.
In my stored procedure if I am simply display the EncryptFirstname as it islike, with out converting to string, no error occurs, but the value I will be getting is system.byte[].
But it needs to be in string format so that I can understand what value it is of?
Please advise, what should I do to display it correctly.
This is the way I am converting the entity to entityViewmodel.
public EmployeeViewModel ConvertFromEmployee(Employee emp)
{
if (emp == null)
return null;
var evm = new EmployeeViewModel();
evm.LastName = emp.Lastname;
evm.EncryptFirstName = emp.EncryptLastName;
return evm;
}