EF Code First 4.1根本不支持nvarchar(max)? [英] EF Code First 4.1 doesn't support nvarchar(max) at all?

查看:652
本文介绍了EF Code First 4.1根本不支持nvarchar(max)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了大量的时间在这个问题上,仍然无法弄清楚为什么EF团队使用代码优先使生活变得如此艰难。



所以这里有一些例子:



我的POCO:



我想要的东西看起来像:

  public class Post 
{
public int Id {get; set;}
public string Text {get; set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Post>()
.Property(p = > p.Text)
.HasColumnType(nvarchar(max));
}

唯一有用的工具:

  public class Post 
{
public int Id {get; set;}

[StringLength(4000)]
public string Text {get;设置;}
}

问题是,在第一种情况下,我尝试插入任何东西它给了我:一个或多个实体的验证失败,第二种情况不符合我的业务模式。



我是唯一有这个问题的人吗?如何处理这个事情?

解决方案

使用 Int32.MaxValue 连接到SQL Server数据库时可能会导致一些问题。在属性或api中使用 Int32.MaxValue 会抛出一个异常,指出不支持MaxLength大于4000的String列。但是,在EF 4.1中使用以下任一方法对我有用:



您可以使用 MaxLengthArritbute 例如

  [MaxLength] 
public string Text {get;组;

或流畅的API,如此

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Post>()
.Property(s => s.Text )
.IsMaxLength();
}

强制使用 ntext 使用以下之一:

  [MaxLength] 
[Column(TypeName =ntext)]
public string Text {get;组;

或流畅的API,如此

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Post>()
.Property(s => s.Text )
.HasColumnType(ntext)
.IsMaxLength();
}

您可能不需要 MaxLength 属性在这种情况下。


I've spent decent amount of time on this problem and still can't figure out why EF team makes the life so hard using Code First.

So here is some sample:

My POCO:

The way I want the thing to look like:

public class Post
{
     public int Id {get; set;}
     public string Text {get; set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Post>()
        .Property(p => p.Text)
        .HasColumnType("nvarchar(max)");   
}

The only thing that works:

public class Post
{
     public int Id {get; set;}

     [StringLength(4000)]
     public string Text {get; set;}
}

The problem is that when in first case I try to insert anything it gives me: Validation failed for one or more entities and the second case doesn't fit my business model.

Am I the only one with this problem? How do I deal with this thing?

解决方案

Using Int32.MaxValue may lead to some problems when connected to a SQL Server database. Using Int32.MaxValue in either the attribute or the api throws an exception stating "String column with MaxLength greater than 4000 is not supported". But, using either of the following methods works fine for me in EF 4.1:

You can use the MaxLengthArritbute, e.g.

[MaxLength]
public string Text { get; set; }

Or the fluent API, like so

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
      modelBuilder.Entity<Post>()
        .Property(s => s.Text)
        .IsMaxLength();
 }

To enforce the use of ntext use one of the following:

[MaxLength]
[Column(TypeName = "ntext")]
public string Text { get; set; }

Or the fluent API, like so

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
      modelBuilder.Entity<Post>()
        .Property(s => s.Text)
        .HasColumnType("ntext") 
        .IsMaxLength();
 }

You may not need the MaxLength attribute in this case.

这篇关于EF Code First 4.1根本不支持nvarchar(max)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆