0

I am using SimpleMemberShip. This is the default UserProfile Model:

[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}

I want to create a Model to get all user details from the Database across multiple Tables:

public class GetUserDetail
{
[Key]
[ReadOnly(true)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }

public int RoleId { get; set; }
public string RoleName { get; set; }

[ReadOnly(true)]
public DateTime CreateDate { get; set; }
public DateTime PasswordChangedDate { get; set; }
}

Currently is seems not to be possible. I am trying to fill a WebGrid for Admin access on my site. I am sure it can be done some how but maybe I am going about it the wrong way.

this is my Controller:

public ActionResult DashBoard()
{
using (var context = new AppContext())
{
return View(context.GetUserDetails.ToList());
}
}

this is my Context Property:

// Account Model DB Set...
public DbSet<GetUserDetail> GetUserDetails { get; set; }

My WebGrid works if I specify the UserProfile as the model source:

@model List<myApp.Models.UserProfile>

but this does not work:

@model List<myApp.Models.GetUserDetail>

Is it possible to implement a custom Model to span multiple already implemented Tables?

Thank You.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55

3 Answers3

3

You will have to manually write queries against your dataContext to retrieve the entities and then send them to the view. Create a ViewModel that has all the properties you need

using (var context = new AppContext())
{
    var userDetailsViewModels = new UserDetailsViewModel
    return View(context.GetUserDetails
                       .Join(dataContext.SomeOtherTable, 
                             u => u.Ut, o => o.UserId, 
                             (u,o) => new UserDetailsViewModel { Name = u.Name, CreatinnDate = o.CreationDate }
                            )
               );
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
0

If you are intereted to learn about custom Model, you may want to take a look at ViewModel. What is viewmodel in mvc

Community
  • 1
  • 1
SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
0

I got this working with some help from Parv Sharma. Although this answer only put me on the right track, my actual solution is quite different.

I added a model:

public class UserProfileData
{
public int UserId { get; set; }
public string UserName { get; set; }

public DateTime CreateDate { get; set; }
public DateTime LastPasswordFailureDate { get; set; }

public DateTime PasswordChangedDate { get; set; }
}

Then I added this code to the Controller:

using (var context = new AppContext())
{
string sqlcommand = "SELECT UserProfile.UserId, UserProfile.UserName, webpages_Membership.CreateDate, webpages_Membership.LastPasswordFailureDate, webpages_Membership.PasswordChangedDate "
+ "FROM UserProfile CROSS JOIN webpages_Membership CROSS JOIN webpages_UsersInRoles "
+ "WHERE (UserProfile.UserId='1') ";

return View(context.Database.SqlQuery<UserProfileData>(sqlcommand).ToList());
}

In my View:

@model List<myApp.Models.UserProfileData>


@{
var grid = new WebGrid(Model);
}

@grid.GetHtml()

I honestly expected this to be a little easier than this. My Issue is that the Tables in the Database for Roles do not have a model associated with them.

Maybe someone else can provide a better solution?

I still need to do some formatting and so on.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55
  • I would like to get rid of the 'WHERE (UserProfile.UserId='1')' and have a result returned with the right user data. – Rusty Nail Sep 13 '13 at 12:56
  • Changing WHERE (UserProfile.UserId='1') to WHERE (UserProfile.UserId=webpages_Membership.UserId) seems to work – Rusty Nail Sep 13 '13 at 12:59
  • SELECT UserProfile.UserId, UserProfile.UserName, webpages_Membership.CreateDate, webpages_Membership.LastPasswordFailureDate, webpages_Membership.PasswordChangedDate, webpages_Roles.RoleName FROM webpages_Membership CROSS JOIN UserProfile CROSS JOIN webpages_UsersInRoles CROSS JOIN webpages_Roles WHERE (UserProfile.UserId=webpages_Membership.UserId) AND (webpages_Roles.RoleId=webpages_UsersInRoles.RoleId) ORDER BY UserProfile.UserId,UserProfile.UserName,webpages_Membership.CreateDate,webpages_Membership.LastPasswordFailureDate,webpages_Membership.PasswordChangedDate, webpages_Roles.RoleName – Rusty Nail Sep 13 '13 at 13:25