dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

Ef Core 3.1 and Identity framework combining DB Context

I am trying to merge my application context with my db context I have tried various solutions but none of them are working for ef core 3.1.

public class AppManagerDBContext : IdentityDbContext
{
    public AppManagerDBContext(DbContextOptions options) : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("name=DefaultConnection");
        }
    }

    public DbSet<BmiInformation> BmiInformation { get; set; }
    public DbSet<WorkOuts> WorkOuts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BmiInformation>().ToTable("BmInformation");            
    }
}

This is the full error that I am getting I thought all I had to do is inherit from IdentityDbContext is there something else I have to do in .NET Core 3.1 and EF Core?

Add-Migration MyFirstMigration -Context AppManagerDBContext
Build started...
Build succeeded.

Error

The AppManagerDBContext constructor must be a DbContextOptions. When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions parameter rather than a non-generic DbContextOptions parameter.

Edit 1 Ok I solved the issue with the following however I am now getting a new error.

My User Info class

public class UserInfo : IdentityUser
{
    // These two new fields are added here
    [PersonalData]
    public string Name { get; set; }
    [PersonalData]
    public DateTime DOB { get; set; }
}

Error 2

The entity type 'IdentityUserLogin' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.

Upvotes: 0

Views: 541

Answers (1)

VRoxa
VRoxa

Reputation: 1051

You must inject a DbContextOptions object into your DbContext, which type paremeter must be the object type you are injecting into.

In your case

public class AppManagerDBContext : IdentityDbContext
{
    public AppManagerDBContext(DbContextOptions<AppManagerDBContext> options) : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // ...
    }

    // ...
}

Both errors are quite explanatory.

You must mark one or more properties of your entity to be a primary key.
If you want so, try

public class UserInfo : IdentityUser
{
    // DatabaseGenerated is optional.
    // Only in case you want to delegate the key generation
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [PersonalData]
    public string Name { get; set; }
    [PersonalData]
    public DateTime DOB { get; set; }
}

Or, if you want none, configure the entity as unkeyed.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BmiInformation>().ToTable("BmInformation");
    modelBuilder.Entity<UserInfo>(u =>
    {
        u.HasNoKey();
    });           
}

Hope it helps.

Upvotes: 1

Related Questions