SQL Server Specify column MaxLength on JSON column in EF Core

06odsfpq  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(185)

I have two classes that represent tables in my database:

public class Parent
{
    public Child Child { get; set; }
}

public class Child 
{
    public string PropertyA { get; set; }
    public string PropertyB { get; set; }
}

I also have this DbContext class

public class SomeContext : DbContext
{
        ...
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>().OwnsOne(x => x.Child, y => y.ToJson());
            base.OnModelCreating(modelBuilder);
        }
}

When I run

dotnet ef migrations add SomeContext_Initial

EF Core generates a migration something like this:

...
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "Parent",
        columns: table => new
        {
            Child = table.Column<string>(type: "nvarchar(max)", nullable: false)
        }
    }
...
}

I use SQL Server as my database. As far as I know, SQL Server automatically moves Child column to pages in the ROW_OVERFLOW_DATA allocation unit because Child column has column type nvarchar(max) even if the content of column is significantly smaller than 8060 bytes ( PropertyA and PropertyB data length in sum is expected to be about 1000 in serialized form).

This is something I clearly don't want for performance reasons.

Even if I specify a length of PropertyA and PropertyB , the migration tool still generates Child column as datatype nvarchar(max) .

I've tried to specify column length using .HasAnnotation("MaxLength", 1000) (or using attribute [MaxLength(1000)] on Child property) and that had no effect.

I've also tried to specify column type nvarchar(1000) and that also had no effect

Is there any way to achieve column length different from nvarchar(max) except editing the migration file manually?

7gyucuyw

7gyucuyw1#

try using this

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("sqlTableName", Schema = "dbo")]
public class Child 
{
    [Column(TypeName = "VARCHAR(MAX)")]
    public string PropertyA { get; set; }

    [Column(TypeName = "VARCHAR(MAX)")]
    public string PropertyB { get; set; }
}

相关问题