如何填充自定义属性表中的ASP.NET使用SimpleMembership当MVC 4 [英] How to Populate Custom Properties Tables When Using SimpleMembership in ASP.NET MVC 4

查看:193
本文介绍了如何填充自定义属性表中的ASP.NET使用SimpleMembership当MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始有3种类型的用户(客户,服务提供商,和管理员)具有不同特定属性的MVC项目。我想延长ASP.NET MVC Web应用程序互联网应用程序模板默认SimpleMembership实现在Visual Studio 2012

I started working on a MVC project which has 3 types of users (customers, service providers, and administrators) with different specific properties. I'd like to extend the default SimpleMembership implementation in ASP.NET MVC Web Application Internet Application template in Visual Studio 2012.

我有Customer类(不知道的关键属性和关系)

I have the Customer class (not sure about the Key attribute and the relationship)

public class Customer
{
    [Key]
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }

    public virtual UserProfile UserProfile { get; set; }
}

和具有客户表中的SQL Server防爆preSS:

and have the Customer table in SQL Server Express:

CREATE TABLE [dbo].[Customer]
(
    [UserName] NVARCHAR(MAX) NOT NULL, 
    [FirstName] NVARCHAR(50) NOT NULL, 
    [LastName] NVARCHAR(50) NOT NULL, 
    [Phone] NVARCHAR(50) NULL
)

实体框架上下文:

Entity Framework Context:

public class UsersContext : DbContext
{
    public UsersContext() : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }

    public DbSet<Customer> Customers { get; set; }
}

什么code,我应该补充下面来获取与用户配置表一起填充客户表?

What code should I add below to get the Customer table populated along with the UserProfile table?

// Attempt to register the user
try
{
     WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

     WebSecurity.Login(model.UserName, model.Password);
     return RedirectToAction("Index", "Home");
 }

在此先感谢

推荐答案

我删除该行从客户类:

public virtual UserProfile UserProfile { get; set; }

客户类现在变成了:

The customer class now becomes:

public class Customer
{
    [Key]
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
}

和增加了​​以下code到的AccountController的注册方法:

and added the following code into the Register method of the AccountController:

try
{
     WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

     Customer cust = new Customer
     {
         UserName = model.UserName,
         FirstName = "Dikembe",
         LastName = "Mutombo",
         Phone = model.Phone
     };                  
     UsersContext context = new UsersContext();
     context.Customer.Add(cust);
     context.SaveChanges();

     WebSecurity.Login(model.UserName, model.Password);
     return RedirectToAction("Index", "Home");
}

这篇关于如何填充自定义属性表中的ASP.NET使用SimpleMembership当MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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