user3587624
user3587624

Reputation: 1471

EF7 Incorrect configuration of the DBContext?

I am trying to figure out if my DBContext is set right. The Web Application I am trying to do is using ASP.NET 5, MVC6 and EF7.

It is connected to a DB that contains 3 tables (Comment, Review, Film). This is my model

WebDbModel.cs

using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WebExample.Models
{
    public partial class Comment : IdentityUser
    {
        public string CommentId { get; set; }
        public string ReviewId { get; set; }
        public string Author { get; set; }
        public System.DateTime Created { get; set; }
    }

    public partial class Review : IdentityUser
    {
        public string ReviewId { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public string Creator { get; set; }
        public string Status { get; set; }
        public Nullable<System.DateTime> CreatedOn { get; set; }
    }

    public partial class Film : IdentityUser
    {
        public string ReviewId { get; set; }
        public string FilmID { get; set; }
        public string CommentId { get; set; }
        public Nullable<int> CommentCount { get; set; }
        public string FilmName { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
        public virtual Review Review { get; set; }
    }
}

Then, I have a class named

RegistrationDbContext.cs

using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;

namespace WebExample.Models
{
    public class ApplicationUser : IdentityUser
    {
    }

    public class RegistrationDbContext : IdentityDbContext<ApplicationUser>
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }

        // Registration of the DB tables we are mapping.
        public DbSet<Comment> Comments { get; set; }
        public DbSet<Review> Reviews { get; set; }
        public DbSet<Film> Films { get; set; }
    }
}

I am wondering from here what is the use of public class ApplicationUser : IdentityUser I am not sure if I should leave it there... If I remove it, then in my Startup.cs, the following code will complain...

// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<RegistrationDbContext>()
        .AddDefaultTokenProviders();

So I don't know to what replace it given the fact I have Comment, Review and Film models...

So my question is... I guess I can delete the ApplicationUser class (auto generated when I created my solution) since this is not part of my model at all but... what should I put instead?

Not too sure if my question is right though, so apologies in advance! This seemed to be changed a lot compared to MVC5, EF6 and I couldn't fine too much documentation around IdentityDbContext

Also... am I missing something else in the RegistrationDbContext.cs class? The only extra thing I added was the registration of the tables... the rest came in the class by default.

Thanks!

Upvotes: 1

Views: 231

Answers (1)

Mark E
Mark E

Reputation: 730

There are a couple of issues here.

Firstly, IdentityUser is the representation of a logged-in user to your site, and is by default represented by the AspNetUsers table in the database, and stores all the usual stuff like email address, password, etc.

The provided ApplicationUser subclass is there in case you want to extend that in any way. Say for example you want to store the date of birth of your users:

public class ApplicationUser : IdentityUser
{
    public DateTime DateOfBirth { get; set; }
}

That will tack on a DateOfBirth column to the AspNetUsers table.

In your case, if you don't want to extend the default user table in any way, you can just delete the ApplicationUser class and replace all references to it in your code to IdentityUser. For example:

// Add Identity services to the services container.
services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<RegistrationDbContext>()
    .AddDefaultTokenProviders();

Secondly, your models should not be inheriting from IdentityUser. That will just add all the fields from the AspNetUsers table to every model, which doesn't make sense, so remove : IdentityUser from your models.

Upvotes: 1

Related Questions