![](/img/trans.png)
[英]My Context does not inherit from DbContext in Entity Framework Core
[英]Entity Framework Core DbContext OnConfiguring does not exist anymore
不久前我使用了 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; }
}
正如您在圖像上看到的, OnConfiguring 應該在 Model 屬性之后,但它不再存在了。
問題:有人知道為什么 OnConfiguring 從 DbContext 中消失了嗎?
在您提供的圖像中,您嘗試使用“public”修飾符進行覆蓋。 OnConfiguring 的覆蓋是“受保護的”。
嘗試編寫“受保護的覆蓋”,intellisense 應該會為您選擇它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.