dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

EF Core 6 not able to complete the migration with unable to create object of type

I am using the latest preview 6 of ef core in a test application to learn the braking changes however. I have a couple of things wrong

using System;
using System.Data.Entity;
using Concierge.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
public class ConciergeDBContext : IdentityDbContext<ApplicationUser>
{
    public ConciergeDBContext(Microsoft.EntityFrameworkCore.DbContextOptions<ConciergeDBContext>
    options)
      : base(options)
    {

    }
    public DbSet<Building> Buildings { get; set; }

    protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

For some reason I have to qualify my model builder with Microsoft.EntityFrameworkCore.DbContextOptions for if I dont i get ambiguity with my DB Set command but that is not all when I compile it I am getting.

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

Now I have my web api project select as start up project and its configured as such

services.AddDbContext<ConciergeDBContext>
(options => 
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")), 
ServiceLifetime.Transient);

So why on earth am i getting the above error.

I am using the command

dotnet ef migrations add InitialCreate -p  Concierge.Dal

When I get the above also outputdir also doesnt appear to be a valid argument in the ef 6 world.

My project structure.

enter image description here

My Concerige.Dal nugets

<ItemGroup>
 <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0-preview.6.21355.2" />
 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.6.21352.1" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.0-preview.6.21352.1" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-preview.6.21352.1">
 <PrivateAssets>all</PrivateAssets>
 <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 </PackageReference>
 <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0-preview.6.21352.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.6.21352.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.6.21352.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

My Context lives within the dal project.

enter image description here

Reason I am using IdentityDbContext is to create custom fields onto my user table so maybe that is part of the issue but this used to work in ef 5?

Upvotes: 1

Views: 1244

Answers (1)

A Farmanbar
A Farmanbar

Reputation: 4798

You need to implement IDesignTimeDbContextFactory

You can also tell the tools how to create your DbContext by implementing the Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory interface: If a class implementing this interface is found in either the same project as the derived DbContext or in the application's startup project, the tools bypass the other ways of creating the DbContext and use the design-time factory instead.

Place the implementation under ConciergeDBContext class. as a result, while migration it will be discovered and invoked.

public class ConciergeDBContext : IdentityDbContext<ApplicationUser>
{
    public ConciergeDBContext(Microsoft.EntityFrameworkCore.DbContextOptions<ConciergeDBContext>
    options)
      : base(options)
    {

    }
    public DbSet<Building> Buildings { get; set; }

    protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
public class ConciergeDBContextFactory : IDesignTimeDbContextFactory<ConciergeDBContext>
{
    public ConciergeDBContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ConciergeDBContext>();
        optionsBuilder.UseSqlServer(_connection_string);

        return new ConciergeDBContext(optionsBuilder.Options);
    }
}

Upvotes: 2

Related Questions