【发布时间】:2012-09-28 20:37:44
【问题描述】:
我正试图围绕所有这些 ViewModel 的东西,并以正确的方式做事。我有几个模型通过实体框架链接到 SQL 表,比如这个:
[Table("HardwareOptions", Schema = "dbo")]
public class HardwareOption {
[Key]
public int RecordID {get; set;}
[ForeignKey("Configuration")]
public string Configuration {get; set;}
public string ComputerManufacturer {get; set;}
public string ComputerModel {get; set;}
public string ComputerDescription {get; set;}
public int MonitorCount {get; set;}
public string MonitorManufacturer {get; set;}
public string MonitorModel {get; set;}
public string MonitorDescription {get; set;}
}
我有一个这样的视图模型:
public class OrderIndexViewModel {
public Customer Customer {get; set;}
public MetaUser MetaUser {get; set;}
public DbSet<HardwareOption> HardwareOptions {get; set;}
public DbSet<Machine> Machines {get; set;}
public DbSet<PendingOrder> PendingOrders {get; set;}
}
还有一些我的控制器是这样的:
private MyDbContext db = new MyDbContext();
OrderIndexViewModel viewmodel = new OrderIndexViewModel();
viewmodel.Customer = db.Customers.Find("myselffortesting");
viewmodel.MetaUser = db.MetaUsers.Find("myselffortesting");
viewmodel.HardwareOptions = db.HardwareOptions;
viewmodel.Machines = db.Machines;
viewmodel.PendingOrders = db.PendingOrders;
return View(viewmodel);
如您所见,在我看来,我只需要有关一位客户/用户的信息,但我需要能够查询整个 HardwareOptions、Machines 和 PendingOrders 表。现在,如果我的架构完全错误,请告诉我,因为这更多的是这个问题的意义所在。但是对于特定的东西,比如说我想从 HardwareOptions 中创建一个下拉列表。从技术上讲,我希望每个SelectItem 都说出几列值的字符串组合,但现在我只想要一个,比如配置。这样做的正确方法是什么?我不知道如何操作DbSet。当我尝试从DbSet 中创建Html.DropDownList 时,我得到了一个包含正确数量项目的列表,但它们都说“mynamespace.Models.HardwareOption”。这是有道理的,我只是不知道如何正确地做到这一点。
【问题讨论】:
-
您的问题的标题非常不具体。请用特定问题替换它:)
-
这是一个关于 DB 设计的问题还是关于 DBSet 有用的问题。
标签: c# asp.net .net asp.net-mvc entity-framework