Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework and string as NCLOB on oracle Db

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?

like image 511
nicecatch Avatar asked Dec 17 '14 11:12

nicecatch


2 Answers

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)

like image 165
nicecatch Avatar answered Nov 01 '22 18:11

nicecatch


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

like image 44
haim770 Avatar answered Nov 01 '22 17:11

haim770