Code first columns with type char(36)

我的梦境 提交于 2019-12-04 03:53:38

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.

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