本文介绍了在的DbContext的EF7不正确的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,如果我的的DbContext设置正确弄清楚。我试图做的Web应用程序是使用ASP.NET 5,MVC6和EF7。

它连接到包含3个表(评论,评论,电影)的DB。这是我的模型

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

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 : IdentityUserI 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!

解决方案

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.

这篇关于在的DbContext的EF7不正确的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:19