You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EF Core 9.0在Azure Synapse建表时遇EXEC语法错误求助

EF Core 9.0连接Azure Synapse执行迁移时“EXEC附近语法不正确”错误的解决方法

尝试使用EF Core 9.0在Azure Synapse中创建表,执行迁移时遇到错误:

第2行第1列解析错误:EXEC附近语法不正确。

相关代码如下:

实体类

public class ActionItemStatus
{
    public int ActionItemStatusId { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
}

DbContext配置

public class SynapseDbContext : DbContext
{
    public DbSet<ActionItemStatus> ActionItemStatus { get; set; }

    public SynapseDbContext(DbContextOptions<SynapseDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ActionItemStatus>(builder =>
        {
            builder.HasKey(p => p.ActionItemStatusId);
            builder.Property(p => p.Name).HasMaxLength(100);
            builder.Property(p => p.IsActive);
            builder.Property(p => p.CreatedDate);
            builder.Property(p => p.ModifiedDate);
        });
    }
}

生成的迁移代码

public partial class CreateTable : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "ActionItemStatus",
            columns: table => new
            {
                ActionItemStatusId = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                IsActive = table.Column<bool>(type: "bit", nullable: false),
                CreatedDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                ModifiedDate = table.Column<DateTime>(type: "datetime2", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_ActionItemStatus", x => x.ActionItemStatusId);
            });
    }

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "ActionItemStatus");
    }
}

Startup配置

builder.Services.AddDbContext<SynapseDbContext>(options =>
{
    var connectionString = configuration.GetConnectionString("AzureSynapseConnectionString");
    options.UseAzureSynapse(connectionString);
});

错误原因

Azure Synapse SQL池(专用/无服务器)与常规SQL Server存在语法和特性差异:

  1. EF Core默认生成的迁移脚本包含Synapse不支持的EXEC语句(如调用SQL Server专属系统存储过程);
  2. datetime2数据类型在Synapse中不被支持,需替换为datetime;
  3. 无服务器SQL池不支持IDENTITY属性,专用池虽支持但EF生成的注解脚本可能存在兼容性问题;
  4. nvarchar(max)在Synapse中建议指定具体长度,避免性能问题。

解决步骤

1. 调整实体类数据类型配置

在OnModelCreating方法中显式指定Synapse兼容的类型:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ActionItemStatus>(builder =>
    {
        builder.HasKey(p => p.ActionItemStatusId);
        builder.Property(p => p.Name).HasMaxLength(100).HasColumnType("nvarchar(100)");
        builder.Property(p => p.IsActive).HasColumnType("bit");
        builder.Property(p => p.CreatedDate).HasColumnType("datetime");
        builder.Property(p => p.ModifiedDate).HasColumnType("datetime");

        // 仅在使用Synapse专用SQL池时保留IDENTITY注解,无服务器池需移除并手动管理主键
        builder.Property(p => p.ActionItemStatusId)
            .HasColumnType("int")
            .Annotation("SqlServer:Identity", "1, 1");
    });
}

2. 更新迁移脚本

重新执行Add-Migration生成新的迁移代码,确保:

  • 所有datetime2替换为datetime;
  • nvarchar(max)替换为nvarchar(100);
  • 无服务器池场景下移除SqlServer:Identity注解。

3. 生成并手动适配SQL脚本

执行Script-Migration命令生成SQL脚本,然后:

  • 删除所有Synapse不支持的EXEC语句(如sp_executesql调用);
  • 调整表创建语句符合Synapse语法(以专用池为例):
CREATE TABLE [ActionItemStatus] (
    [ActionItemStatusId] INT IDENTITY(1,1) NOT NULL,
    [Name] NVARCHAR(100) NOT NULL,
    [IsActive] BIT NOT NULL,
    [CreatedDate] DATETIME NOT NULL,
    [ModifiedDate] DATETIME NOT NULL,
    CONSTRAINT [PK_ActionItemStatus] PRIMARY KEY CLUSTERED ([ActionItemStatusId])
);

4. 在Synapse中执行脚本

登录Azure Synapse Studio,打开查询编辑器连接目标SQL池,执行调整后的SQL脚本完成表创建。

5. 验证连接字符串

确保使用Synapse专用连接字符串格式(以专用池为例):

Server=tcp:<你的Synapse服务器名>.sql.azuresynapse.net,1433;Database=<你的数据库名>;User ID=<用户名>;Password=<密码>;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

内容的提问来源于stack exchange,提问作者Sonal Khatri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 00:42:03