问题
I have this model:
public class Teacher
{
public int TeacherID { get; set; }
public string Name { get; set: }
public string Surname{ get; set; }
}
and when Model First run, it creates my Teachers table and DbSet, but for Name and Surname (which are string) it assigns NCLOB type to the column. Now, with NCLOB type I cannot do some operations like equals or distincts on my table....
How can I force MF to set columntype to varchar?
回答1:
I've managed to solve the issue setting the maximum string lenght into the model
public class Teacher
{
public int TeacherID { get; set; }
[StringLength(255, MinimumLength = 3, ErrorMessage = "My Error Message")]
public string Name { get; set: }
[StringLength(255, MinimumLength = 3, ErrorMessage = "My Error Message")]
public string Surname{ get; set; }
}
Without the StringLength Orcale creates a NCLOB field that can contain up to 4Gb of data.
Note: Maximum lenght for varchar is 4000 bytes, so we cannot set more than 2000 as MaximumLenght (2 byte per character with Unicode)
回答2:
Try to configure it explicitly:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Teacher>().Property(x => x.Name).HasColumnType("varchar");
modelBuilder.Entity<Teacher>().Property(x => x.Surname).HasColumnType("varchar");
}
See Documentation
来源:https://stackoverflow.com/questions/27525078/entity-framework-and-string-as-nclob-on-oracle-db