问题
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