在的DbContext的EF7不正确的配置? [英] EF7 Incorrect configuration of the DBContext?

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

问题描述

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

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


  

WebDbModel.cs


 使用Microsoft.AspNet.Identity.EntityFramework;
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;命名空间WebExample.Models
{
    公共部分类点评:IdentityUser
    {
        公共字符串CommentId {搞定;组; }
        公共字符串ReviewId {搞定;组; }
        公共字符串作者{搞定;组; }
        公众的System.DateTime创建{搞定;组; }
    }    公共部分类评价:IdentityUser
    {
        公共字符串ReviewId {搞定;组; }
        公共字符串名称{;组; }
        公共字符串作者{搞定;组; }
        公共字符串造物主{搞定;组; }
        公共字符串状态{搞定;组; }
        公众可空<&System.DateTime的GT; CreatedOn {搞定;组; }
    }    公共部分类电影:IdentityUser
    {
        公共字符串ReviewId {搞定;组; }
        公共字符串FilmID {搞定;组; }
        公共字符串CommentId {搞定;组; }
        公众可空< INT> CommentCount {搞定;组; }
        公共字符串FilmName {搞定;组; }
        公共虚拟的ICollection<&评论GT;评论{搞定;组; }
        公共虚拟点评{搞定;组; }
    }
}

然后,我有一个名为类


  

RegistrationDbContext.cs


 使用Microsoft.AspNet.Identity.EntityFramework;
使用Microsoft.Data.Entity;命名空间WebExample.Models
{
    公共类ApplicationUser:IdentityUser
    {
    }    公共类RegistrationDbContext:IdentityDbContext< ApplicationUser>
    {
        保护覆盖无效OnModelCreating(模型构建器生成器)
        {
            base.OnModelCreating(制造商);
            //自定义ASP.NET身份模型,如果需要,覆盖默认值。
            //例如,您可以重命名ASP.NET身份表名等等。
            //调用base.OnModelCreating(建造者)后,添加自定义;
        }        //我们正在映射数据库表的登记。
        公共DbSet<&评论GT;评论{搞定;组; }
        公共DbSet<回顾与GT;评论{搞定;组; }
        公共DbSet<薄膜>电影{搞定;组; }
    }
}

我在这里有什么用疑惑的公共类ApplicationUser:IdentityUser
我不知道我是否应该离开那里......如果我删除它,然后在我的Startup.cs,下面code会抱怨...

  //的服务容器添加身份服务。
services.AddIdentity< ApplicationUser,IdentityRole>()
        .AddEntityFrameworkStores< RegistrationDbContext>()
        .AddDefaultTokenProviders();

所以我不知道什么替代它鉴于我的注释 查看电影机型...

所以我的问题是...我想我可以在所有的删除ApplicationUser类(当我创造了我的解决方案自动生成),因为这不是我的模型的一部分​​,但是......我应该把呢?

也不太清楚,如果我的问题是正确的,所以事先的歉意!这似乎比起MVC5,EF6被改变了很多,我不能左右精太多文档的 IdentityDbContext

另外...我失去了在 RegistrationDbContext.cs 类的东西?我说是表登记的唯一的额外......其余的默认排在了班级。

谢谢!


解决方案

有几个问题在这里。

首先, IdentityUser 是重新presentation登录的用户到你的网站,并且是由<$ C psented默认回复$ P $ $ C> AspNetUsers 数据库中的表,并存储所有常用的东西,如电子邮件地址,密码等。

提供的 ApplicationUser 子类中有您要扩展,在任何情况下的方式。例如说,你要存储用户的出生日期:

 公共类ApplicationUser:IdentityUser
{
    公众的DateTime DATEOFBIRTH {搞定;组; }
}

这将钉在一个 DATEOFBIRTH 列到 AspNetUsers 表。

在你的情况,如果你不希望延长默认的用户表中的任何方式,你可以删除 ApplicationUser 类和替换所有引用它您code到 IdentityUser 。例如:

  //的服务容器添加身份服务。
services.AddIdentity&LT; IdentityUser,IdentityRole&GT;()
    .AddEntityFrameworkStores&LT; RegistrationDbContext&GT;()
    .AddDefaultTokenProviders();

第二,您的模型不应该从 IdentityUser 继承。这将刚刚从<$ ​​C $ C> AspNetUsers 表中的所有字段添加到每一个模型,这是没有意义的,所以删除:IdentityUser 从模型。

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!

解决方案

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不正确的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆