Answer the question
In order to leave comments, you need to log in
How to display related data in Entity Framework?
Something I'm already confused about the Entity Framework. I'm learning EF, I made a database using Model-first, a piece of the database on the screen .
How to display fields in datagridveiw, for example, title, price, and category name?
If used ProductGrid.DataSource = db.ProductSet.ToList();
then it outputs System.Data.Entity.DynamicProxies.Category.
Answer the question
In order to leave comments, you need to log in
I would do like this:
1) I would declare a view class
class ProductDto
{
[System.ComponentModel.DisplayName("ID")]
[System.ComponentModel.Browsable(false)]
public int ProductId { get; set; }
[System.ComponentModel.DisplayName("Наименование")]
public string ProductName { get; set; }
[System.ComponentModel.DisplayName("Цена")]
public float ProductPrice { get; set; }
[System.ComponentModel.DisplayName("Категория")]
public string Category { get; set; }
[System.ComponentModel.DisplayName("Категория (ID)")]
[System.ComponentModel.Browsable(false)]
public int CategoryId { get; set; }
}
var dtos = (db.ProductSet.Include(x=>x.Category)
.Select(x => new ProductDto {
ProductId = x.ProductId,
ProductName = x.ProductName,
ProductPrice = x.ProductPrice,
Category = x.Category.CategoryName,
CategoryId = x.Category.CategoryId
})).ToArray();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question