我在 MVC4 中使用 simplemembership,效果很好,但是我需要添加其他用户详细信息,例如名字和姓氏、电子邮件、国家/地区,每个用户可以有多个配置文件,所以我从 UserProfile 表中的用户表添加 ID {FK} . 用户注册时我有单独的表格,系统调用另一个表格询问其他详细信息。我正在使用代码优先现有数据库方法
我错过了如何存储附加信息以及使用 LINQ 对其用户配置文件进行存储的难题。
提前谢谢了 ..
数据库:
CREATE TABLE [dbo].[UserProfile] (
[UserId] INT IDENTITY (1, 1) NOT NULL,
[UserName] NVARCHAR (150) NOT NULL,
[ID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([UserId] ASC),
FOREIGN KEY ([ID]) REFERENCES [dbo].[Users] ([ID])
);
CREATE TABLE [dbo].[Users] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Email] NVARCHAR (150) NOT NULL,
[First_Name] NVARCHAR (150) NOT NULL,
[Last_Name] NVARCHAR (150) NOT NULL,
[Country] NVARCHAR (150) NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);
控制器类
[HttpGet]
[Authorize]
public ActionResult UserDetail()
{
return View();
}
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult UserDetail(User model)
{
using (var db = new DB5Context_01())
{
var user_info = new User
{
First_Name = model.First_Name,
Last_Name = model.Last_Name,
Country = model.Country,
Email = model.Email
};
//// need help here ??? hock Userprofile with User
}
SimpleMemberShip 初始化
namespace Simple_LoginSystem_05.Filters
{
public class InitializeSimpleMembership : ActionFilterAttribute
{
public InitializeSimpleMembership()
{
try
{
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
模型
public partial class User
{
public User()
{
this.UserProfiles = new HashSet<UserProfile>();
}
public int ID { get; set; }
public string Email { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Country { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }