我的背景是:
public class RegistrationManagerContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("aspnet_Users");
}
}
我的用户类是:
[Table("aspnet_Users")]
public class User
{
[Key]
[Display(AutoGenerateField = true)]
[Required]
public Guid UserId { get; set; }
[DataType("nvarchar")]
[MaxLength(256)]
[Required]
public string UserName { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, ConvertEmptyStringToNull = true, DataFormatString = "{0:d}")]
[Required]
public DateTime LastActivityDate { get; set; }
[NotMapped]
public AccountProfile Profile
{
get
{
if (_profile == null)
{
_profile = AccountProfile.GetUserProfile(this.UserName);
}
return _profile;
}
}
private AccountProfile _profile = null;
}
我的调用方法(控制器)是:
namespace RegistrationManager.Controllers
{
[Authorize(Roles="Admins")]
public class AdminController : Controller
{
private RegistrationManagerContext db = new RegistrationManagerContext();
//
// GET: /Admin/
public ActionResult Index(int? id)
{
var viewModel = db.Users;
if (id.HasValue)
{
ViewBag.UserID = id.Value;
}
return View(viewModel);
}
}
}
我的看法是:
@model IEnumerable<RegistrationManager.Models.User>
@{
ViewBag.Title = "Users";
}
<h2>Users</h2>
<table>
<tr>
<th>Email Address</th>
<th>Given Names</th>
<th>Surname</th>
<th>Last Ac</th>
<th>Refresh</th>
</tr>
@foreach (var item in Model)
{
string selectedRow = "";
if (ViewBag.UserId != null && item.UserId == ViewBag.UserId)
{
selectedRow = "selectedrow";
}
<tr class="@selectedRow" valign="top">
<td>
@item.UserName
</td>
<td>
@item.Profile.GivenNames
</td>
<td>
@item.Profile.Surname
</td>
<td>
@String.Format("{0:d}", item.LastActivityDate)
</td>
<td>
@Html.ActionLink("Refresh", "Refresh", "Admin", new { id = item.UserId })
</td>
</tr>
}
</table>
那么我做错了什么或者我错过了什么?因为我没有数据!???肯定有数据,因为我至少应该看到我自己的登录信息,对吧!?