Code first columns with type char(36)

瘦欲@ 提交于 2019-12-05 19:20:17

问题


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?


回答1:


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

[StringLength( 36 )]
[Column( TypeName = "char" )]
public string UserIdentifier{ get; set; }



回答2:


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.



来源:https://stackoverflow.com/questions/14876121/code-first-columns-with-type-char36

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!