gpt4 book ai didi

c# - Entity Framework Core 2 迁移问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:43 25 4
gpt4 key购买 nike

我正在使用 ASP.NET Core 2 构建应用程序,数据库部分是 EF core 2。

我有这样的模型

public class User        
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

这是上下文文件

public class EFCoreDbContext:DbContext
{
public EFCoreDbContext(DbContextOptions<EFCoreDbContext> options)
: base(options)
{

}

public DbSet<ChatMessage> ChatMessages { get; set; }
}

最后是 startup.cs

services.AddDbContext<EFCoreDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MooltiRoomDatabase")));

和 appsettings.json 中的连接字符串

"ConnectionStrings": {
"MooltiRoomDatabase": "Server=DESKTOP-BCQ6IAU\\CHATDB,Database=MultiRoomChat;Trusted_Connection=True;"
}

通过包管理器控制台,我正在运行 Add-Migration 命令成功完成,尽管事实上在 DB 中它没有创建相应的表,之后我读到我需要运行 Update-Database 创建表的命令,当我运行这个命令时,我得到了这样的错误

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)

最佳答案

正如您在错误消息中看到的那样,“连接字符串无效。”你用了“,”而不是“;”在数据库之前。

现在你有:

"Server=DESKTOP-BCQ6IAU\\CHATDB,Database=MultiRoomChat;Trusted_Connection=True;"

但是,你应该使用这个:

"Server=DESKTOP-BCQ6IAU\\CHATDB;Database=MultiRoomChat;Trusted_Connection=True;"

关于c# - Entity Framework Core 2 迁移问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com