【发布时间】:2017-07-24 19:27:42
【问题描述】:
我正在使用 Code First 方法学习实体框架。我有两个具有一对多关系的类:
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
//Foreign Key -----> To establish relationship btw two tables.
public int CategoryID { get; set; }
// A product belongs to a ----> Category
public virtual ProductCategory Category { get; set; }
}
public class ProductCategory
{
[Key] //PK
public int CategoryID { get; set; }
public string CategoryName { get; set; }
// A Category ----> has many products (List of Products)
public virtual List<Product> ProductList { get; set; }
}
我的 DbContext 类:
class ModelDBEntitiesContext2 : DbContext
{
public ModelDBEntitiesContext2()
{
//turns off easy loading for all Entities---> use Eager loading to load related Entities by using "Include"
this.Configuration.LazyLoadingEnabled = false;
}
public DbSet<Product> products { get; set; }
public DbSet<ProductCategory> categories { get; set; }
}
我使用 linq 方法语法使用 INCLUDE (Eager Loading) 字词查询所有产品和所有产品及其类别,如下所示:
ModelDBEntitiesContext2 db = new ModelDBEntitiesContext2();
var pppp = db.products.Include(c => c.Category).ToList();
//I bind it to a datagrid view
dataGridView.DataSource = pppp;
dataGridView 上的两个查询结果显示客户的列返回System.Data.Entity.DynamicProxies.ProductCategory
DataGridView Result
我到底做错了什么?为什么即使使用包含我也无法检索包含所有产品的类别。
【问题讨论】:
-
但确实如此!由于它是一个复杂类型,它将显示一个类别的字符串表示。您应该设置列的正确字段名称,例如 CategoryName。
-
将类别列绑定到类似
Eval("Category.CategoryName") -
@PeterBons @Robert McKee 我确实试过这个:
var pppp = db.products.Include(p => p.Category.CategoryName).ToList();但有一个例外,The EntityType 'EFcodeFirst2.ModelEF.ProductCategory' does not declare a navigation property with the name 'CategoryName'. -
需要修复的不是EF查询,是datagridview列绑定。
-
@PeterBons 我不这么认为,因为我只是在控制台应用程序中尝试过,我得到了相同的结果“System.Data.Entity.DynamicProxies.ProductCategory”dropbox.com/s/7fl0kl8j8hbeq39/lazy2.PNG?dl=0
标签: c# entity-framework linq datagridview