为什么在 Entity Framework Core 中找不到 DbConnect
Why DbConnect is not found in Entity Framework Core
我正在使用 EF Core 开发一个测试 Web 项目。当 运行 使用 DbContext 进行测试时,我 运行 遇到以下问题。
以下是我的开发环境信息:
- ASP.NET 核心 3.1
- Entity Framework 核心 3.1
- IDE: Visual Studio 2019
- 平台:Windows 10 台电脑
主项目名称 OdeToFood
和一个 class 库项目 OdeToFood.Data
用于使用 Entity Framework 核心进行数据访问。当 运行 执行以下命令时,我收到以下错误消息:
C:\Projects\OdeToFood\OdeToFood.Data>dotnet ef dbcontext info -s ..\odetofood\odetofood.csproj
Build started...
Build succeeded.
Unable to create an object of type 'OdeToFoodDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
到目前为止,我还没有找到解决这个问题的方法。任何帮助或建议将不胜感激。以下部分是相关的代码段和配置设置。
在odetofood项目属性中,启动项目已设置为OdeToFood
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>
- 在OdeToFood.data项目中,DbContext定义如下:
namespace OdeToFood.Data
{
public class OdeToFoodDbContext: DbContext
{
public OdeToFoodDbContext(DbContextOptions<OdeToFoodDbContext> options)
: base(options)
{
}
public DbSet<Restaurant> Restaurats { get; set; }
}
}
- 在主项目
OdeToFood
下的appsettings.json
中,DbConnection
字符串输入如下:
"ConnectionStrings": {
"OdeToFoodDb": "Server=DESKTOP-E7P6N4O; Database=OdeToFoodDb; user id=OdeToFoodDbUser; password=xxxxxx; Encrypt=false"
}
- 在 Startup.cs 中,为
DbContext
添加了服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<OdeToFoodDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("OdeToFoodDb"));
});
services.AddSingleton <IRestaurantData, InMemoryRestauantData>();
services.AddRazorPages();
}
向您的 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; }
}
}
我正在使用 EF Core 开发一个测试 Web 项目。当 运行 使用 DbContext 进行测试时,我 运行 遇到以下问题。
以下是我的开发环境信息:
- ASP.NET 核心 3.1
- Entity Framework 核心 3.1
- IDE: Visual Studio 2019
- 平台:Windows 10 台电脑
主项目名称 OdeToFood
和一个 class 库项目 OdeToFood.Data
用于使用 Entity Framework 核心进行数据访问。当 运行 执行以下命令时,我收到以下错误消息:
C:\Projects\OdeToFood\OdeToFood.Data>dotnet ef dbcontext info -s ..\odetofood\odetofood.csproj
Build started...
Build succeeded.Unable to create an object of type 'OdeToFoodDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
到目前为止,我还没有找到解决这个问题的方法。任何帮助或建议将不胜感激。以下部分是相关的代码段和配置设置。
在odetofood项目属性中,启动项目已设置为OdeToFood
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>
- 在OdeToFood.data项目中,DbContext定义如下:
namespace OdeToFood.Data
{
public class OdeToFoodDbContext: DbContext
{
public OdeToFoodDbContext(DbContextOptions<OdeToFoodDbContext> options)
: base(options)
{
}
public DbSet<Restaurant> Restaurats { get; set; }
}
}
- 在主项目
OdeToFood
下的appsettings.json
中,DbConnection
字符串输入如下:
"ConnectionStrings": {
"OdeToFoodDb": "Server=DESKTOP-E7P6N4O; Database=OdeToFoodDb; user id=OdeToFoodDbUser; password=xxxxxx; Encrypt=false"
}
- 在 Startup.cs 中,为
DbContext
添加了服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<OdeToFoodDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("OdeToFoodDb"));
});
services.AddSingleton <IRestaurantData, InMemoryRestauantData>();
services.AddRazorPages();
}
向您的 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; }
}
}