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?
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With