Entity Framework and string as NCLOB on oracle Db

倖福魔咒の 提交于 2020-01-14 10:03:15

问题


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

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