小编Kla*_*naZ的帖子

DbContext 的 C# 问题:参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions”

我有一些问题DbContext。我决定创建一个新类,它将负责与DataBase 的连接。

public class PetAlertContext : DbContext
{
    public PetAlertContext() : base("PetAlert") { }

    public DbSet<Zwierze> Zwierzaki { get; set; }
        public DbSet<Osoba> Osoby { get; set; }
        public DbSet<Placowka> Placowki { get; set; }
        public DbSet<Ogloszenie> Ogloszenia { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我在基地名称附近有一些例外:

参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions”。

我发现了类似的问题和类似这样的代码的响应,但我在理解这种结构时遇到了一些问题。

你会这么好心帮我吗?我评论了这些:

public PetAlertContext(string connectionString) : base(GetOptions(connectionString))
{
}

private static DbContextOptions GetOptions(string connectionString)
{
    return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
}
Run Code Online (Sandbox Code Playgroud)

c# database string dbcontext entity-framework-core

2
推荐指数
1
解决办法
5836
查看次数

标签 统计

c# ×1

database ×1

dbcontext ×1

entity-framework-core ×1

string ×1