So I have a UserProfile model class as part of SimpleMembership. In it I need to store a legacy identifier that exists in another DB of type char(36)
. I'd love to change this to something more sensible like a uniqueIdentifier but that's out of scope for today's activities.
My current annotation creates a column nvarchar(36)
[StringLength(36)]
public string UserIdentifier{ get; set; }
I'd like a column of char(36)
instead. Is this possible?
If you want to keep with Data Annotations, then just simply use:
[StringLength( 36 )]
[Column( TypeName = "char" )]
public string UserIdentifier{ get; set; }
Ok. I found the answer myself.
If I create the following configuration class for my UserProfile:
class UserProfileConfiguration:EntityTypeConfiguration<UserProfile>
{
public UserProfileConfiguration()
{
this.Property(p => p.UserIdentifier)
.HasMaxLength(36)
.IsFixedLength()
.IsUnicode(false);
}
}
then override OnModelCreating
in my DbContext
to add this configuration:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new UserProfileConfiguration());
}
then I'm in business and I get a char(36)
column. Yay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With