Trying to have a model with key which is string but in database will be represented as byte[].
Is there any way i can do it?
There is no direct translation from string to a byte array. You have to specify in what encoding the byte array will represent the string.
You can add a non-mapped wrapper property (TheStringValue below) that uses a specific encoding to map the string to bytes and vice versa:
public class MyEntity
{
public byte[] StringBytes { get; set; }
[NotMapped]
public string TheStringValue
{
get
{
return Encoding.UTF8.GetString(StringBytes);
}
set
{
StringBytes = Encoding.UTF8.GetBytes(value);
}
}
}
[NotMapped] - NotMapped attribute can be applied to properties of a class. Default Code-First convention creates a column for all the properties which includes getters and setters. NotMapped attribute overrides this default convention. You can apply NotMapped attribute to a property which you do NOT want to create a column in a database table for. more info