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

Entity Framework Core 7.1报错:不支持'server'关键字

问题解决:EF Core 7.1 连接Azure SQL时出现"不支持'fatqtyfkag.database.windows.net; initial catalog'关键字"错误

错误原因

你的代码中OnConfiguring方法里的SQL Server连接字符串仅填写了"server",这是完全不完整、格式错误的连接参数,导致EF Core无法正确解析Azure SQL的连接配置,进而抛出关键字不支持的错误。

解决方案

  1. 使用完整的Azure SQL连接字符串
    Azure SQL的标准连接字符串格式如下,你需要将占位符替换为实际的数据库信息:
Server=tcp:fatqtyfkag.database.windows.net,1433;Initial Catalog=你的数据库名称;Persist Security Info=False;User ID=你的数据库用户名;Password=你的数据库密码;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
  1. 修正DbContext代码
    将完整的连接字符串传入UseSqlServer方法,修改后的代码示例:
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Emp.Data
{
    public class EmpDataContext : DbContext
    {       
        public DbSet<DisplayAPIDataEmployee>? DisplayAPIDataEmployees {get; set;}

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 替换为你的完整Azure SQL连接字符串
            var connectionString = "Server=tcp:fatqtyfkag.database.windows.net,1433;Initial Catalog=你的数据库名称;Persist Security Info=False;User ID=你的用户名;Password=你的密码;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
            optionsBuilder.UseSqlServer(connectionString);
        }

        protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DisplayAPIDataEmployee>().ToTable("Employees", e => e.IsTemporal());
        }
    }
}
  1. 优化建议(可选)
    为避免硬编码敏感信息(如密码),建议从配置文件读取连接字符串:
// appsettings.json
{
  "ConnectionStrings": {
    "EmpDb": "你的完整Azure SQL连接字符串"
  }
}

之后通过依赖注入获取配置并传入:

public class EmpDataContext : DbContext
{
    private readonly IConfiguration _configuration;

    public EmpDataContext(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public DbSet<DisplayAPIDataEmployee>? DisplayAPIDataEmployees {get; set;}

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(_configuration.GetConnectionString("EmpDb"));
    }

    // OnModelCreating代码保持不变
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 02:55:15