[英]ASP.NET Core WebApi Add-Migration Entity Framework Core
I'm trying to build a ASP.NET Core WebApi with Entity Framework Core and AutoMapper.我正在尝试使用 Entity Framework Core 和 AutoMapper 构建 ASP.NET Core WebApi。 When i try to use Add-Migration
i get Exception has been thrown by the target of an invocation.当我尝试使用Add-Migration
时,我得到异常已被调用目标抛出。 at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
(I haven't made any changes to the Program class).在Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
(我没有对 Program 类进行任何更改)。 I think the problem may be that the program cannot find the connection string.我认为问题可能是程序找不到连接字符串。
This is my DataBaseContext 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; }
}
This is appsettings.json:这是 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"
}
}
This is Startup.cs:这是 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
}
}
This is someproject.csproj:这是 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>
Because your project is a 3.1 version,your packages因为你的项目是 3.1 版本,所以你的包
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.2" />
are not compatible.不兼容。
You need change it to:您需要将其更改为:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.11" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.