日期:2014-05-17 浏览次数:20503 次
public class USER { [Key] public int USERID { get; set; } public string NAME { get; set; } public string DEPTID{ get; set; } public string TYPEID{ get; set; } public virtual DEPT depts { get; set; } public virtual USERTYPE usertypes{ get; set; } } public class DEPT { [Key] public string DEPTID{ get; set; } public string NAME{ get; set; } public virtual ICollection<USER> users { get; set; } } public class USERTYPE { [Key] public string TYPEID{ get; set; } public string NAME{ get; set; } public virtual ICollection<USER> users { get; set; } }
public class USERDB:DbContext { public DbSet<USER> users { get; set; } public DbSet<DEPT> depts { get; set; } public DbSet<USERTYPE> usertypes{ get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<USER>() .HasRequired(u => u.depts) .WithMany(d => d.users) .HasForeignKey(a => a.DEPTID) .WillCascadeOnDelete(false); modelBuilder.Entity<USER>() .HasRequired(u => u.usertypes) .WithMany(t => t.users) .HasForeignKey(a => a.TYPEID) .WillCascadeOnDelete(false); } }
public ActionResult TestList() { var db=new USERDB(); var users = db.users.ToList(); return View(users); }
@model IEnumerable<testlist.Models.USER> @foreach (var item in Model) { <tr> <td> @item.ID </td> <td> @item.NAME </td> <td> @item.depts.NAME //<--------这里报错 </td> <td> @item.usertypes.NAME </td> <td>