Reputation: 37
I'm trying to build a ASP.NET Core WebApi with Entity Framework Core and AutoMapper. When i try to use Add-Migration
i get Exception has been thrown by the target of an invocation. at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
(I haven't made any changes to the Program class). I think the problem may be that the program cannot find the connection string.
This is my 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:
{
"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:
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:
<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>
Upvotes: 1
Views: 751
Reputation: 7200
Because your project is a 3.1 version,your packages
<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" />
Upvotes: 2