C# ASP.NET核心WebApi添加迁移实体框架核心
我正在尝试使用实体框架核心和AutoMapper构建一个ASP.NET核心WebApi。当我尝试使用C# ASP.NET核心WebApi添加迁移实体框架核心,c#,asp.net-core,entity-framework-core,automapper,C#,Asp.net Core,Entity Framework Core,Automapper,我正在尝试使用实体框架核心和AutoMapper构建一个ASP.NET核心WebApi。当我尝试使用addmigration时,调用的目标抛出了异常。在Microsoft.EntityFrameworkCore.Tools.Program.Main(String[]args)(我没有对程序类做任何更改)。我认为问题可能是程序找不到连接字符串 这是我的DataBaseContext类: public class DataBaseContext : DbContext {
addmigration
时,调用的目标抛出了异常。在Microsoft.EntityFrameworkCore.Tools.Program.Main(String[]args)
(我没有对程序类做任何更改)。我认为问题可能是程序找不到连接字符串这是我的DataBaseContext类:
public class DataBaseContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DataBaseContext"));
}
public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)
{
}
public DbSet<Director> Directors { get; set; }
public DbSet<Movie> Movies { get; set; }
}
这是Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataBaseContext>(opt =>
opt.UseSqlServer("DataBaseContext"));
var config = new AutoMapper.MapperConfiguration(c =>
{
c.AddProfile(new MapperProfile());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
services.AddSingleton<IUserService, UserService>();
services.AddControllers();
}
//some code
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(opt=>
opt.UseSqlServer(“DataBaseContext”);
var config=new AutoMapper.MapperConfiguration(c=>
{
c、 AddProfile(新的MapperProfile());
});
var mapper=config.CreateMapper();
服务。AddSingleton(映射器);
services.AddSingleton();
services.AddControllers();
}
//一些代码
}
}
这是someproject.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
</ItemGroup>
</Project>
netcoreapp3.1
全部的
运行时间;建设;本地人;内容文件;分析仪;可传递的
因为您的项目是3.1版本,所以您的软件包
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.2" />
不兼容
您需要将其更改为:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.11" />
您试过了吗?
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.11" />