【问题标题】:ASP.NET Relation between two tables两个表之间的 ASP.NET 关系
【发布时间】:2016-10-08 16:01:32
【问题描述】:

我是 Asp.Net 和 Entity Framework 的新手,我有一个小问题。

我知道你可以帮助我,因为它很简单,我只知道如何在 PHP XD 中做到这一点

我有两个模型

 public class Suppliers
 {
    public int ID { get; set; }

    public int ID_Guard { get; set; }

    public string Supplier{ get; set; }
    public string Description { get; set; }
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm tt}", ApplyFormatInEditMode = true)]

    public DateTime Enter { get; set; }
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm tt}", ApplyFormatInEditMode = true)]

    public DateTime Exit { get; set; }

    public virtual Guard Guard { get; set; }
}



public class Guard
{
    public int ID { get; set; }

    public string Name { get; set; }
    public virtual ICollection<Suppliers> Suppliers{ get; set; }

}

控制器

public class SuppliersController : Controller
{
    private SuppliersContext db = new SuppliersContext();

    public ActionResult Index()
    {
        return View(db.Suppliers.ToList());
    }
}

我想将 2 个表数据传递给视图并将其关联起来

当我进入索引时,显示所有suppliers 数据并显示 Guard 名称(注册供应商的人进入和退出)

解决了

 var results = from c in db.Suppliers
                      join cn in db.Guard on c.ID_Guard equals cn.ID

                      where (c.ID_Guard == cn.ID)
                      select new Models.MyViewModel{ Name= cn.Name, ID = c.ID, Supplier=c.Supplier, Description=c.Description, Enter=c.Enter, Exit=c.Exit};

【问题讨论】:

    标签: c# sql asp.net has-many


    【解决方案1】:

    按照下面的代码,它可以工作,看起来和你的一样,我添加了链接希望你觉得它有帮助

     public class Product
    {
        public Product() { Id = Guid.NewGuid(); }
        public Guid Id { get; set; }
        public string ProductName { get; set; }
    
        public virtual ProductCategory ProductCategory { get; set; }
    }
    
    
    public class ProductCategory
    {
        public int Id { get; set; }
        public string CategoryName { get; set; }
        public virtual ICollection<Product> Products { get; set; }
    }
         /////view model///
    public class ProductViewModel
    {
        public Guid Id { get; set; }
    
        [Required(ErrorMessage = "required")]
        public string ProductName { get; set; }
    
        public int SelectedValue { get; set; }
    
        public virtual ProductCategory ProductCategory { get; set; }
    
        [DisplayName("Product Category")]
        public virtual ICollection<ProductCategory> ProductCategories { get; set; }
    

    }

    按照下面的教程 relationship model

    基本连接使用下面的查询

    var dealercontacts = from contact in table1
                     join table2 in table2 on contact.Id equals another.ID
                     select contact;
    

    【讨论】:

    • 感谢您的回答,我应该把这些方法放在哪里?在控制器上对吗?在视图中我不需要使用@model?
    • 它在“a.values,c.values”上给了我一个错误错误 CS1922 无法使用集合初始化程序初始化类型“MyViewModel”,因为它没有实现“System.Collections.IEnumerable”
    • 并给我错误“Context.Guard”它说在当前上下文中不存在
    • 它对我有用,但仍然不知道如何在我的索引视图中显示
    • 我无法通过 sqlquery 来加入 Suppliers.ID_Guard=Guard.ID 的两个表
    猜你喜欢
    • 1970-01-01
    • 2016-06-27
    • 2017-07-31
    • 2018-09-03
    • 2020-09-14
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多