实体框架核心DbContext OnConfiguring不再存在

时间:2020-10-07 13:30:26

标签: c# entity-framework-core

前一段时间,我使用了Entity Framework Core,所以我不知道他们是否只是删除了DbContext类或程序集中的虚拟OnConfiguraing方法或您所谓的方法。但是我到处都有搜索,他们没有答案。 我曾尝试从V17和V19进行更改,但两者都相同。

我要做什么: 使用实体框架,我正在尝试代码优先方法。
下面的代码可以正常工作。

(摘自docs.microsoft) 使用Microsoft.EntityFrameworkCore;

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public int Rating { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

我能得到的 disappearance of onconfig

正如您在图像上看到的,OnConfiguring应该位于Model属性之后,但现在不再存在。

问题:有人知道为什么DbContext中消失了OnConfiguring吗?

1 个答案:

答案 0 :(得分:0)

在您提供的图像中,您尝试使用“ public”修饰符覆盖。 OnConfiguring的替代是“受保护的”。

尝试编写“受保护的替代”,然后智能感知将为您接上它。