I began to study MVC3 development and hit a stumbling block a bit. In my project, I moved the default aspnet_ tables to a separate SQL Server 2008 Express database. Then I added my own tables to store additional profile data. Now I would like to create a view that lists usernames (from aspnet_Users), email addresses (from aspnet_Membership), First and Last Name (from the UserDetails user table with userid as PK and FK in aspnet_Users) and the user role (aspnet_UsersInRoles and aspnet_Roles).
So far, reading and writing from my database, I have built models for each of my custom tables with the dbcontext class:
public class UserDetail { [Key] public Guid UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } public string Phone { get; set; } public DateTime DateUpdated { get; set; } } public class UserLinkToken { [Key] public Guid UserId { get; set; } public string LinkToken { get; set; } public DateTime DateUpdated { get; set; } } public class ProShotDB : DbContext { public DbSet<UserDetail> UserDetails { get; set; } public DbSet<UserLinkToken> UserLinkTokens { get; set; } }
I had to call my tables "UserDetails" and "UserLinkTokens" for my application to write and update my database, when the names of the models and dbcontext did not match the names of my database and tables, the application would finish creating another database with tables and write there is data.
From what I can say, to send an object for viewing using the properties from the tables and the aspnet_ properties from my user tables, I want to create a ModelView. After reading many examples of the model, I still do not understand how this should look for my application.
Can someone give me an example of a model that I could use to display a list of data rows and update all the relevant columns of the table referenced in the model view, taking into account the user ID?
In this example, we can say that I just wanted to see all the FirstNames tables (UserDetails) and their corresponding letters (aspnet_Membership).
My ultimate goal is a presentation that displays users with assigned roles. Each registered user will have a link that will lead to a controller that will designate them as an administrator if they are not currently in this role or remove the administrator role if they are currently administrators.
Sorry if this question was asked earlier in other questions. I read a lot of them and I feel that they probably should have answered my question, but none of them seemed to match. Most likely, my confusion stems from an incomplete understanding of how my application connects to my database. I understand that my models are a data row for a table, and dbcontext takes a set of these data row models and connects them to a table in the database (dbset <(datarow / model object)> (datarows table / set of model objects)). Since I don't see the dbcontext class for aspnet_ tables / model objects, I don't have a good concept on how to retrieve, update, or write data in these tables.
Thanks for any help, clarifying my ignorance about how this works or gives me a simple example of how to accomplish what I need.