10

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?

spender
  • 117,338
  • 33
  • 229
  • 351
  • The answer to this [question](http://stackoverflow.com/questions/6760765/how-do-i-map-a-char-property-using-the-entity-framework-4-1-code-only-fluent-a) may help. – zs2020 Feb 14 '13 at 13:44
  • @sza No it didn't. I found a way. Thanks anyway. – spender Feb 14 '13 at 14:05

2 Answers2

13

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

[StringLength( 36 )]
[Column( TypeName = "char" )]
public string UserIdentifier{ get; set; }
krzychu
  • 3,577
  • 2
  • 27
  • 29
10

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.

spender
  • 117,338
  • 33
  • 229
  • 351