c# - Store data in LINQ to two tables; Join LINQ -
i using simplemembership mvc4 work great, required add addition user details first , last name, email, country constraints each user can have multiple profiles add id {fk} users table in userprofile table. have seprate form when user register, system call form asking addition details. using code first existing database approach
i missing puzzle of how store additional information along saying hock user profile using linq.
many in advance ..
database:
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) );
controller class
[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 here ??? hock userprofile user }
simplemembership initializing
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 not initialized. more information, please see http://go.microsoft.com/fwlink/?linkid=256588", ex); } } } }
model
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; }
simplemembership expects single "userprofile" per user, stores in table specify in call to:
websecurity.initializedatabaseconnection("defaultconnection", "userprofile", "userid", "username", autocreatetables: true);
in default code table called "userprofile", matches userprofile
in accountmodels.cs
. see detail section 4 of my answer question more information userprofile
.
i should note if wasn't requirement "each user can have multiple profiles" add properties such email, firstname, lastname, country userprofile
class. move class out of userscontext
own context, delete userscontext
, update references userscontext
own context in accountcontroller
, initializesimplemembershipattribute
(you set email login token instead of username
using approach).
so, while don't understand how single user needs more 1 firstname etc., assuming have reason instance going have introduce new terms, like:
userprofile
- keep meaning simplemembershipuserprofile
make easier read documentation. there 1 record per login in table.attributesforuser
- use describe requirement "additional user details first , last name, email, country constraints each user can have multiple profiles". seems correspondsuser
table above. using nameuser
table cause clashesuser
membership ,websecurity
framework methods , properties maybe not best idea. each entry in tableuserprofiles
can have 1 or more entries in tableattributesforusers
.
it looks have code-first details sorted. example of public virtual icollection<userprofile> userprofiles { get; set; }
correct, change to:
public virtual icollection<attributesforuser> attributesforuser { get; set; }
and attributesforuser
(which add dbset data context) like:
public class attributesforuser { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int id { get; set; } public int userid { get; set; } [required] public string firstname { get; set; } [required] public string lastname { get; set; } [required] public string country { get; set; } [required] public string email { get; set; } [foreignkey("userid")] public userprofile userprofile { get; set; } }
when create user, can do:
using (mycontextdb = new mycontext()) { websecurity.createuserandaccount(model.username, model.password); // assumes have moved userprofile mycontext, alter code accordingly if not userprofile userprofile = db.userprofiles.firstordefault(u => u.username.tolower() == model.username.tolower()); attributesforuser newattribs = new attributesforuser { userid = userprofile.userid, ... fill in other properties here ... }; db.attributesforusers.add(newattribs); // many more newattribs need userid ... // , save db.savechanges(); }
Comments
Post a Comment