Use nvarchar datatype in SQL Server CE with EF code-first

761 Views Asked by At

I use SQL Server CE with EF code-first. I have following domain class:

public class User:BaseEntity
{
    public User()
    {
        this.UserForms = new List<UserForm>();
        IsAdmin = false;
        IsActive = true;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool IsAdmin { get; set; }
    public bool IsActive { get; set; }

    public virtual ICollection<UserForm> UserForms { get; set; }
}

and following mapping class:

 public class UserMap : EntityTypeConfiguration<User>
 {
        public UserMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.FirstName)
                .HasMaxLength(25)
                .IsRequired();

            this.Property(t => t.LastName)
                .HasMaxLength(30)
                .IsRequired();

            this.Property(t => t.UserName)
                .HasMaxLength(10)
                .IsRequired();

            this.Property(t => t.Password)
                .HasMaxLength(30)
                .IsRequired();
        }
    }

EF creates the following table for me:

enter image description here

EF use ntext instead of nvarchar to make tables and when I run the app I get the following error:

The ntext and image data types cannot be used in WHERE, HAVING, GROUP BY, ON, or IN clauses, except when these data types are used with the LIKE or IS NULL predicates

How can i fix this problem?

1

There are 1 best solutions below

0
On

have you tried to force the column type?

this.Property(t => t.FirstName)
.HasColumnType("nvarchar")
.HasMaxLength(25)
.IsRequired();