我试图弄清楚我的DBContext是否设置正确。我尝试做的Web应用程序正在使用ASP.NET 5,MVC6和EF7。
它连接到包含3个表(评论,评论,电影)的DB。这是我的模特
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; }
}
}
然后,我有一个名为
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; }
}
}
我从这里想知道公共类ApplicationUser:IdentityUser的用途是什么?我不确定是否应该将其保留在其中...如果删除它,那么在我的Startup.cs中,以下代码将抱怨...
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<RegistrationDbContext>()
.AddDefaultTokenProviders();
因此,鉴于我拥有Comment,Review和Film模型,因此我不知道该用什么替代...
所以我的问题是...我想我可以删除ApplicationUser类(在创建解决方案时自动生成),因为这根本不是模型的一部分,但是...我应该放什么呢?
不太确定我的问题是否对,请您提前道歉!与MVC5,EF6相比,这似乎发生了很大变化,并且我对IdentityDbContext的文档了解过多
另外...我在RegistrationDbContext.cs类中缺少其他内容吗?我添加的唯一一件事是表的注册...默认情况下,其余部分都在类中。
谢谢!
这里有几个问题。
首先,IdentityUser
是您网站登录用户的表示形式,默认情况下AspNetUsers
是数据库中的表表示的形式,它存储了所有常用的内容,例如电子邮件地址,密码等。
ApplicationUser
如果您想以任何方式进行扩展,则可以使用提供的子类。假设您要存储用户的出生日期:
public class ApplicationUser : IdentityUser
{
public DateTime DateOfBirth { get; set; }
}
这将添加DateOfBirth
到AspNetUsers
表格的一列。
就您而言,如果您不希望以任何方式扩展默认用户表,则可以删除ApplicationUser
该类,并将代码中的所有对它的引用替换为IdentityUser
。例如:
// Add Identity services to the services container.
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<RegistrationDbContext>()
.AddDefaultTokenProviders();
其次,您的模型不应继承IdentityUser
。这样只会将AspNetUsers
表格中的所有字段添加到每个模型中,这没有意义,因此请: IdentityUser
从模型中删除。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句