【发布时间】:2013-07-23 18:08:40
【问题描述】:
我在 MVC4 中使用 simplemembership,效果很好,但是我需要添加其他用户详细信息,例如名字和姓氏、电子邮件、国家/地区,每个用户可以有多个配置文件,所以我从用户表中添加 ID {FK}在 UserProfile 表中。 用户注册时我有单独的表格,系统调用另一个表格询问附加详细信息。我正在使用代码优先现有数据库方法
我错过了如何存储附加信息以及使用 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; }
【问题讨论】:
-
你的表名是不是混淆了?不可能是对的
-
我猜不是...我说的是每个用户可以有许多 UserProfile,但每个 UserProfile 属于一个用户
-
只需创建一个 List
并为属于该用户的每个 UserProfile 填写它 -
如果我提出正确的问题,我该如何做 LINQ 部分
-
@toxic,很难理解你的问题部分是“一边说 hocking its user profile”,你能编辑一下吗?
标签: c# linq simplemembership