模型类:
public class User {
[Key]
public Guid UserId { get; set; }
[Display(Name = "User Name")]
public string UserName { get; set; }
public virtual ICollection<Asp_Membership> CreateDates { get; set; }
}
public class Asp_Membership {
[Key]
public Guid UserId { get; set; }
[Display(Name = "Date Created")]
public DateTime CreateDate { get; set; }
}
数据库上下文
public class UsersContext : DbContext {
public UsersContext() : base("ApplicationServices") { }
public DbSet<User> Users { get; set; }
public DbSet<Asp_Membership> Asp_Memberships { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
//mapping of Users and Membership
//what exactly do i put here to join the two tables by UserId
}
我的问题很简单,您可能会说,这是我第一次尝试了解 EF 的实际工作原理。我要做的是加入基于 UserId 的 aspnet_Users 表和 aspnet_Membership 表。我该怎么做?
但更重要的是,我真的找不到好的实体框架教程,有什么建议吗?
- 更新 -
DbContext(同上)
型号类:
public class User {
[Key]
public Guid UserId { get; set; }
[Display(Name = "User Name")]
public string UserName { get; set; }
public virtual Asp_Membership asp_Membership { get; set; }
}
public class Asp_Membership {
[Key]
public Guid UserId { get; set; }
[Display(Name = "Date Created")]
public DateTime CreateDate { get; set; }
}
控制器:
private UsersContext db = new UsersContext();
// GET: /Users/Index
public ActionResult Index() {
return View(db.Users.ToList());
}
索引页面:(删除了不必要的元素......)
@model IEnumerable<ReservationPro.Models.User>
...
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.asp_Membership.CreateDate)
</td>
</tr>
}
我要做的就是弄清楚如何从默认的 aspnetdb 中加入两个单独的表“aspnet_Users”和“aspnet_Membership”。如模型类中所述,它们都有“UserId”列。我知道这是一个简单的问题,我很抱歉让这个混乱。正如您可能知道的那样,我刚刚开始使用 EF,只是想用一些简单的代码让我的手“变脏”。