MVC 5 Code First首先使用UserId

时间:2017-02-14 16:01:32

标签: c# sql-server database entity-framework

现在我的IdentityModels.cs看起来像这样:

using System;
using System.Data.Entity;
using System.Security.Claims;
using System.Security.Policy;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Leepio.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string City { get; set; }
        //company
        [Display(Name = "Virkshomedsnavn")]
        public string CompanyName { get; set; }
        public string ZipCode { get; set; }
        //company
        public int NrEmployees { get; set; }
        //company
        public string WorkField { get; set; }
        public string Language { get; set; }
        //Student
        public string University { get; set; }
        public string StudyProgramme { get; set; }
        public int Semester { get; set; }
        public string GraduationDate { get; set; }
        //
        [AllowHtml]
        public string Description { get; set; }
        //Student
        public string Skills { get; set; }
        public string Website { get; set; }
        public string Address { get; set; }
        //Student
        [DataType("date")]
        public DateTime DateOfBirth { get; set; }
        public virtual ICollection<Blog> Blogs { get; set; }
        public virtual ICollection<Application> Applications { get; set; }
        public virtual ICollection<Project> Project { get; set; }
        public virtual IList<Experience> Experience { get; set; }
        public virtual ICollection<Skill> Skill { get; set; }
        public virtual IList<Education> Education { get; set; }

        public virtual IEnumerable<Experience> ExperienceOrdered { get { return Experience.OrderByDescending(e => e.EndYear); } }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {

            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        public System.Data.Entity.DbSet<Leepio.Models.Project> Projects { get; set; }
        public System.Data.Entity.DbSet<Leepio.Models.NewsletterMails> NewsletterMails { get; set; }
        public System.Data.Entity.DbSet<Skill> Skill { get; set; }
        public System.Data.Entity.DbSet<Leepio.Models.Application> Applications { get; set; }
        public System.Data.Entity.DbSet<Leepio.Models.Contract> Contracts { get; set; }
        public System.Data.Entity.DbSet<Leepio.Models.Experience> Experience { get; set; }
        public System.Data.Entity.DbSet<Leepio.Models.Blog> Blogs { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Education> Educations { get; set; }
    }
}

我想要做的是,通过代码(迁移),通过代码添加一个新表作为用户表(AspNetUsers)的扩展,以便我在注册新用户时从Facebook获取数据,我的数据不需要为主用户表可以转到第二个,让我们称之为“FBData”与相应的userId。 从透视角度来看: 注册用户将添加userID,FirstName,LastName,Email to Users表,同时在FBData表中添加ID和Locale,Gender。

2 个答案:

答案 0 :(得分:0)

Microsoft Identity使用AspNetUserClaims存储您在ApplicationUser模型类中指定的所有其他数据。当您使用外部身份验证提供程序(如Facebook,Google等)时,会在AspNetUserLogins表中创建一个新条目来存储ProviderKey。当用户第二次登录您的应用程序时,将使用此密钥。

据我了解,您创建FBData表的计划不一定好。你之后添加Google身份验证怎么样?你会创建一个GData表吗?

最好的选择是在您的AccountController中的ExternalLoginCallback(当用户从​​您的外部身份验证提供程序重定向到您的页面时触发的功能)将您从Facebook收到的内容映射到您的ApplicationUser,并可能将用户重定向到注册表单使用预先填充的字段来完成注册周期。您可以在单独的表AspNetUserClaims_External中存储所有垃圾数据,但您必须先对其进行建模。

检查: https://github.com/IdentityServer/IdentityServer4.Samples/tree/dev/Quickstarts/4_ImplicitFlowAuthenticationWithExternal

答案 1 :(得分:0)

您需要做一些事情。首先,您可以使用该表中所需的属性创建FbData类。然后将所需的属性添加到ApplicationUser类。您需要FbData表的外键

public int FbDataId { get; set; }

您还需要为表添加虚拟属性:

public virtual ICollection<FbData> FbDatas { get; set; }

最后在您的IdentityContext中为您的表添加DbSet:

public DbSet<FbData> FbData { get; set; }

在干净的数据库上运行应用程序以查看您的更改已反映..

您还可以覆盖IdentityContext中的OnModelCreating类,以对生成的身份表进行任何修改。