ApplicationUser和Controllers的外键

时间:2016-02-22 22:20:28

标签: asp.net entity-framework asp.net-mvc-3 asp.net-identity

我遇到了类似的问题。在我的模型中建立关系后,我在Controller(创建和编辑)中出现错误。我尝试了一些东西,但似乎没有解决问题。有任何想法吗?谢谢!

型号:

public class JbsTest{

public int Id { get; set; }
public string Description { get; set; }
public double Duration { get; set; }

public int ProjectId { get; set; }
public virtual Project Project { get; set; }

public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
}

控制器:

public ActionResult Create()
{
    ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name");
    ViewBag.UserId = new SelectList(*db.ApplicationUsers*, "Id", "Name");
    return View();
}

此行显示错误:

ViewBag.UserId = new SelectList(*db.ApplicationUsers*, "Id", "Name");

ApplicationDbContext类:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<TestProject5.Models.JbsTest> JbsTests { get; set; }
}

错误消息: enter image description here

0 个答案:

没有答案