Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code first columns with type char(36)

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?

like image 849
spender Avatar asked Feb 14 '13 13:02

spender


2 Answers

If you want to keep with Data Annotations, then just simply use:

[StringLength( 36 )]
[Column( TypeName = "char" )]
public string UserIdentifier{ get; set; }
like image 81
krzychu Avatar answered Oct 22 '22 07:10

krzychu


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.

like image 40
spender Avatar answered Oct 22 '22 08:10

spender