I have a class library project containing the entity models and DBContexts. Having trouble getting the ConnectionString from the UI project's appsettings.json file.
Here's a couple of things I've tried (please note my C# and Web dev knowledge is pretty low level):
1. services.AddDbContext added to ConfigureServices method in UI.Web project.
//Project: Entities.EF
//Type: Class Library (.NET CORE 3.1)
//DataAccess/ProductCategoriesContext.cs
using Entities.EF.Models.Products.Categories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
namespace Entities.EF.DataAccess
{
public class ProductCategoriesContext : DbContext
{
public ProductCategoriesContext(DbContextOptions<ProductCategoriesContext> options) : base(options) { }
public DbSet<ProductModel_Category_Tier1> Categories_Tier1 { get; set; }
public DbSet<ProductModel_Category_Tier2> Categories_Tier2 { get; set; }
public DbSet<ProductModel_Category_Tier3> Categories_Tier3 { get; set; }
public DbSet<ProductModel_Category_Type> Category_Types { get; set; }
}
}
//Project: UI.Web
//Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Entities.EF.DataAccess;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace WebApp
{
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.AddRazorPages();
services.AddDbContext<ProductCategoriesContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("Default"));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
//Project: UI.Web
//Sappsettings.json
{"ConnectionStrings": {"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDB2;Integrated Security=True;"
},"Logging": {"LogLevel": {"Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"
}
},"AllowedHosts": "*"
}
Error:
//Package Manager Console
//Default Project set to : Entities.EF
PM> add-migration InitialDB
Unable to resolve startup project ''.
Using project 'Entities\Entities.EF' as the startup project.
Build started...
Build succeeded.
System.IO.FileLoadException: Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)PM>
2. Changed class library to target framework : .NET CORE 2.2
//Package Manager Console //Default Project set to : Entities.EF
PM> add-migration InitialDB
Unable to resolve startup project ''.
Using project 'Entities\Entities.EF' as the startup project.
Build started...
Build succeeded.Unable to create an object of type 'ProductCategoriesContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
PM>
3. The only way to get it to work was by removing services.AddDbContext() from ConfigureServices in UI.Web/Startup.cs and using the OnConfiguring method inside the DBContext:
//Project: Entities.EF
//Type: Class Library (.NET CORE 2.2)
//DataAccess/ProductCategoriesContext.cs
using Entities.EF.Models.Products.Categories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
namespace Entities.EF.DataAccess
{
public class ProductCategoriesContext : DbContext
{
//public ProductCategoriesContext(DbContextOptions<ProductCategoriesContext> options) : base(options) { }
#region CATEGORIES
public DbSet<ProductModel_Category_Tier1> Categories_Tier1 { get; set; }
public DbSet<ProductModel_Category_Tier2> Categories_Tier2 { get; set; }
public DbSet<ProductModel_Category_Tier3> Categories_Tier3 { get; set; }
public DbSet<ProductModel_Category_Type> Category_Types { get; set; }
#endregion
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True;");
}
}
}
So... does migraton only work if the class library is targeted at .NET CORE 2.2?
And how can I get ConnectionString without explicity declaring it in the Class Library project?
Any help would be greatly appreciated.