【问题标题】:Why DbConnect is not found in Entity Framework Core为什么在 Entity Framework Core 中找不到 DbConnect
【发布时间】:2022-01-21 17:21:06
【问题描述】:

我正在使用 EF Core 开发一个测试 Web 项目。使用 DbContext 运行测试时遇到以下问题。

以下是我的开发环境信息:

  • ASP.NET Core 3.1
  • 实体框架核心 3.1
  • IDE:Visual Studio 2019
  • 平台:Windows 10 电脑

主项目名称OdeToFood 和一个类库项目OdeToFood.Data 用于使用Entity Framework Core 进行数据访问。运行以下命令时收到以下错误消息:

C:\Projects\OdeToFood\OdeToFood.Data>dotnet ef dbcontext info -s ..\odetofood\odetofood.csproj

构建开始...
构建成功。

无法创建“OdeToFoodDbContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

到目前为止,我还没有找到解决这个问题的方法。任何帮助或建议将不胜感激。以下部分是相关的代码段和配置设置。

  1. 在 odetofood 项目属性中,启动项目已设置为 OdeToFood

  2. OdtToFood.csproj 文件内容:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
      <TargetFramework>netcoreapp3.1</TargetFramework>
      <StartupObject>OdeToFood.Program</StartupObject>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>
    <ItemGroup>
        <ProjectReference Include="..\OdeToFood.Data\OdeToFood.Data.csproj" />
    </ItemGroup>
</Project>
  1. 在 OdeToFood.data 项目中,DbContext 定义如下:
namespace OdeToFood.Data
{
    public class OdeToFoodDbContext: DbContext
    {
        public OdeToFoodDbContext(DbContextOptions<OdeToFoodDbContext> options)
            : base(options)
        {

        }
        public DbSet<Restaurant> Restaurats { get; set; }
    }
}
  1. 在主项目OdeToFood下的appsettings.json中,输入DbConnection字符串如下:
"ConnectionStrings": {
    "OdeToFoodDb": "Server=DESKTOP-E7P6N4O; Database=OdeToFoodDb; user id=OdeToFoodDbUser; password=xxxxxx; Encrypt=false" 
}   
  1. 在 Startup.cs 中,为 DbContext 添加服务:
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContextPool<OdeToFoodDbContext>(options => {
    options.UseSqlServer(Configuration.GetConnectionString("OdeToFoodDb"));
    });
    services.AddSingleton <IRestaurantData, InMemoryRestauantData>();
    services.AddRazorPages();
}

【问题讨论】:

  • 当你说你正在使用3.1时,为什么你的项目文件中有:Version="2.2.0"
  • @Poul Bak:抱歉,我发错了。

标签: c# asp.net-core web entity-framework-core dbcontext


【解决方案1】:

将构造函数添加到您的 OdeToFoodDbContext 将解决问题。

我的测试结果

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Data
{
    public class OdeToFoodDbContext : DbContext

    { 
        public OdeToFoodDbContext()
        {
        }

        public OdeToFoodDbContext(DbContextOptions options)
           : base(options)
        {

        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"...;");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Restaurant>().ToTable("Restaurant");
        }
        public DbSet<Restaurant> Restaurats { get; set; }
    }
        public class Restaurant {
        [Key]
        public int Restaurats { get; set; }
    }
}

【讨论】:

  • 我按照您的示例进行了以下代码更改:
  • 我按照您的代码示例在 OdeToFoodDbContext 中进行了代码更改。运行命令:dotnet ef dbcontext info ..\odetofood\odetofood.csproj。它没有错误地完成,没有显示任何 DbConext 信息。看起来有些东西工作不正常。 (由于我是 Stackoverflow 的新手,我找不到添加更多代码段的方法。)还有其他建议吗?
  • @bedrock 您要查看哪些信息?看来这个问题已经解决了。如果您想从 stackoverflow 获得快速响应,您需要创建一个新帖子来提出另一个问题。
  • @bedrock 通常,提问的帖子会很快得到解决。当其他用户看到当前帖子有答案时,他们通常会忽略它。并且请注意添加适当的标签。
  • @bedrock 如果我的回复有帮助并解决了当前问题,请接受它作为答案(单击回复旁边的标记选项将其从灰色切换为填写。),请参阅@987654322 @
猜你喜欢
  • 2019-07-19
  • 2021-08-28
  • 1970-01-01
  • 2021-06-11
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
相关资源
最近更新 更多