【问题标题】:Use nvarchar datatype in SQL Server CE with EF code-first在 SQL Server CE 中使用 nvarchar 数据类型和 EF 代码优先
【发布时间】:2013-11-11 01:19:10
【问题描述】:

我使用 SQL Server CE 和 EF 代码优先。我有以下域类:

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; }
}

以及以下映射类:

 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 为我创建了下表:

EF 使用ntext 而不是nvarchar 来制作表格,当我运行应用程序时出现以下错误:

ntext 和 image 数据类型不能用于 WHERE、HAVING、GROUP BY、ON 或 IN 子句,除非这些数据类型与 LIKE 或 IS NULL 谓词

我该如何解决这个问题?

【问题讨论】:

  • 您检查的数据库文件是否正确?
  • 是的。我有一个空的数据库文件
  • 什么 ef 版本?您是否尝试过使用属性即。最大长度(30)?
  • @ErikEJ Entity Framework 6. 不,我没有尝试 MaxLength 属性。我用 this.Property(t => t.FirstName) .HasMaxLength(25) .IsRequired();
  • 我已经用MaxLengthStringLength 属性复制了这个问题,并将其作为映射属性。 HasColumnType 似乎没有任何效果。非常令人沮丧,我还没有解决方法。

标签: entity-framework ef-code-first sql-server-ce nvarchar ntext


【解决方案1】:

您是否尝试过强制列类型?

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

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多