如何在asp.net mvc

时间:2017-04-02 15:59:30

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我研究了主键,候选键和外键的每个位置。我在asp.net MVC 5实体框架上学习了每一个键。

然后我在Visual Studio上使用MVC自动生成选项生成了我的代码,我在迁移时被编辑了一些功能。但是,这就是......

我创建了另一个名为Auction的模型,以便为一个项目出价。之后,我在我的拍卖模型中初始化了产品ID和其他详细信息。即使在那之后,我也尝试使用Microsoft.AspNet.Identity来分配用户ID; 它的工作原理。问题是我想在自动生成的用户模型之间建立一个名为“IdentityModels”和“AccountViewModels”的关系。但它无效。

// Here is my Auction model class' segment that I took on my Auction model


[Key]
 [Required]
 public long Id { get; internal set; }
........
//other codes are avoided
......
[Required]
public string SellerUserID { get; set; }

这是我的AuctionController

 public ActionResult ViewMyBids()
        {
            var db = new AuctionsDataContext();
            var pKey = User.Identity.GetUserId();

            // I want to pass the primary key in to the auction model and get the results to a list 
            var result = db.Auctions.Find().SellerUserID(pKey);
            ViewBag.mes = result.SellerUserID;

            return ViewMyBids();

        }

即使我不知道如何获得主键的价值 我们举个例子说: 有 员工和部门...... 员工有身份证, 员工有EmpName, 部门有一个DepartmentName,

部门想知道某个员工的姓名...... 这样做,

部门表需要一个与员工ID相关的外键, 在该employeeID中,它可以直接分配给存储在Employee表中的员工姓名......

因此,就像我想要分配自动生成的用户ID并使用该ID一样,我需要检索ID具有的所有数据....

如何做到这一点,我知道使用外键或组合键..但在我的研究中,我无法找到任何解决方案。

所以帮我解决这个问题。谢谢....... PLS ...

1 个答案:

答案 0 :(得分:0)

public ActionResult ViewMyBids()
{
    var db = new AuctionsDataContext();
    var pKey = User.Identity.GetUserId();

    // I want to pass the primary key in to the auction model and get the results to a list 
    var result = db.Auctions
        .Where(p => p.SellerUserID == pKey)
        .ToList();

    // The result is a List<Auction> so the below code won't work unless you retrieve only one item
    // ViewBag.mes = result.SellerUserID;

    return ViewMyBids();
}