0

I am starting to lean asp.net core 3 and I have an issue at present. I am trying to share my db context like I used to through a .net core dll but I have an error.

I have the following declared in my class.

// This method gets called by the runtime. Use this method to add services to e container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddDbContext<DbContext_Model>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("AppliedContext")));
}

The db context which I have in my core dll project solution.

public class DbContext_Model: DbContext
{
    public DbContext_Model(DbContextOptions<DbContext_Model> options)
      : base(options)
    { }

    public DbSet<Customer> Customer { get; set; }
    public DbSet<Policys> Policys { get; set; }

}

I have declared my connection string below with password masked

"ConnectionStrings": { "AppliedContext": "Server=DESKTOP\MSSQLSERVER2017;Database=Systems;User Id=sa;Password=xxxxx;MultipleActiveResultSets=true"

But When I do Add-Migration InitalCreate I get this error.

Unable to create an object of type 'DbContext_Model'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Is sharing of the context no longer allowed between .net core libarys and a web site ?

Edit 2

Please see screen shot which shows I am selecting the correct project. enter image description here

Edit 3

That seemed to cause an another error the suggestion below. So I added the nugets in for the json tooling but still get this error.

enter image description here

dotnet ef migrations add InitialCreate System.MissingMethodException: Method not found: 'System.Text.Json.JsonDocument System.Text.Json.JsonDocument.Parse(System.IO.Stream, System.Text.Json.JsonReaderOptions)'. at Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute() at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.b__0() at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args) at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args) Method not found: 'System.Text.Json.JsonDocument System.Text.Json.JsonDocument.Parse(System.IO.Stream, System.Text.Json.JsonReaderOptions)'. PM> dotnet ef migrations add InitialCreate

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • You’re probably trying to create the migrations with the wrong project. You will have to use the one that actually _configures_ the context. Otherwise, the migration tool won’t be able to construct it. At least if you want to configure it that way. – poke Aug 29 '19 at 22:36
  • Hi Please see screen shot that i am selecting the shared libary – c-sharp-and-swiftui-devni Aug 29 '19 at 22:55

3 Answers3

0

Try something like this:

options.UseSqlServer(
    Configuration.GetConnectionString("AppliedContext"),
    options => options.MigrationsAssembly(<DB_PROJECT>)

Then you should be able to run this from within the folder that has the DbContext project:

dotnet ef migrations add <NAME> --startup-project <STARTUP_PROJECT>
crgolden
  • 4,332
  • 1
  • 22
  • 40
  • Oh ok - I didn't know you were using 3.0. Try this: https://stackoverflow.com/a/56487123 – crgolden Aug 29 '19 at 23:22
  • Try running `dotnet tool install --global dotnet-ef --version 3.0.0-*` to update your `dotnet ef` tools then. – poke Aug 30 '19 at 05:14
0

From the screenshot in your Edit3, your need to update EF Core related package to version 3.0 . In Manage NutGet Packages , check Include prerelease next to the search box and update them to latest version like below :

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0-preview8.19405.11" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview8.19405.11" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview8.19405.11">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
</ItemGroup>

</Project>

Note :When adding migration , set your web application as the Startup project and set the class library as the default project where you want Migration folder generates.

Reference :https://dotnetthoughts.net/using-ef-core-in-a-separate-class-library/

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36
0

The screenshot of your Edit3 seems to indicate that your EF tool version is outdated. You can check by executing:

dotnet ef --version

on the command line. If it's not preview8, then install it:

dotnet tool update dotnet-ef --version 3.0.0-preview8.19405.11

(optionally use the --global switch).

Beware: EF preview8 has an additional issue when used with HasData. It should be fixed in preview9 (which at the time of this writing is not out yet).

Dejan
  • 9,150
  • 8
  • 69
  • 117