ASP.NET 核心 WebApi 添加迁移 Entity Framework 核心
ASP.NET Core WebApi Add-Migration Entity Framework Core
我正在尝试使用 Entity Framework Core 和 AutoMapper 构建 ASP.NET Core WebApi。当我尝试使用 Add-Migration
时,我得到 Exception has been throwed by the target of an invocation. at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
(我没有对程序进行任何更改class)。我认为问题可能是程序找不到连接字符串。
这是我的 DataBaseContext Class:
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; }
}
这是appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DataBaseContext": "Server=DESKTOP-LLPVCRN\KISIELSQL;Database=KredekMovieManagement;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
这是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
}
}
这是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>
因为你的项目是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" />
我正在尝试使用 Entity Framework Core 和 AutoMapper 构建 ASP.NET Core WebApi。当我尝试使用 Add-Migration
时,我得到 Exception has been throwed by the target of an invocation. at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
(我没有对程序进行任何更改class)。我认为问题可能是程序找不到连接字符串。
这是我的 DataBaseContext Class:
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; }
}
这是appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DataBaseContext": "Server=DESKTOP-LLPVCRN\KISIELSQL;Database=KredekMovieManagement;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
这是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
}
}
这是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>
因为你的项目是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" />