[英]Entity Framework 5.0 code first one to one and one to many relationship
I have two entities- Product and Picture . 我有两个实体 - 产品和图片 。 The idea is that a product can have only one picture or none and a picture can be mapped to many products. 这个想法是产品只能有一张图片或者没有图片,而且图片可以映射到许多产品。
public class Product
{
public int Id {get; set;}
public Picture Picture { get; set; }
public bool Enabled { get; set; }
public string Text { get; set; }
}
The Picture entity is a mapping to another Pictures entity that contains all of the pictures (this Picture entity contains a mapping only for the pictures for the products), so basically it contains only two columns with Ids. 图片实体是到包含所有图片的另一个图片实体的映射(此图片实体仅包含产品图片的映射),因此基本上它只包含两个带有ID的列。
public class Picture
{
public int Id {get; set;}
public int PictureId { get; set; }
}
This is my model binder for the Product entity 这是我的Product实体的模型绑定器
modelBuilder.Entity<Product>().HasOptional<Picture>(pr => pr.Picture);
When I create a Product entity with picture, or add a picture to an existing entity the Picture_Id column that is created in the Product table is correctly populated with the Id from the Picture table. 当我创建带有图片的Product实体,或者将图片添加到现有实体时,在Product表中创建的Picture_Id列使用Picture表中的Id正确填充。 The problem is when I try to retrieve the Product . 问题是当我尝试检索产品时 。 The Picture entity inside the Product entity is null. Product实体内的Picture实体为null。 I suspect that the mapping is incorrect. 我怀疑映射是不正确的。 Can you tell me how to populate the Picture entity inside the Product entity when I retrieve it. 你能告诉我如何在检索它时在Product实体中填充Picture实体。 Here is the retrieval code: 这是检索代码:
public Product GetProductById(int productId)
{
var query = from pr in _productRepository.Table
where pr.Id == productId
select pr;
return query.FirstOrDefault();
}
EDIT: I am using custom IRepository service that I include with dependency injection 编辑:我使用自定义IRepository服务,我包含依赖注入
IRepository<Product>
The IRepository interface contains the following methods: IRepository接口包含以下方法:
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
IQueryable<T> Table { get; }
This is the implementation of Table : 这是表的实现:
public virtual IQueryable<T> Table
{
get
{
return this.Entities;
}
}
private IDbSet<T> Entities
{
get
{
if (_entities == null)
_entities = _context.Set<T>();
return _entities;
}
}
I can not add .Include("Picture") to the Table , because it is IQueryable . 我无法在表中添加.Include(“Picture”) ,因为它是IQueryable 。 I also enabled the Lazy Loading, but there were no result. 我也启用了延迟加载,但没有结果。
var products = db.Pictures.Include("Product");
public Product GetProductById(int productId)
{
var query = from pr in products
where pr.Id == productId
select pr;
return query.FirstOrDefault();
}
you should use include for loading related entities : 你应该使用include来加载相关的实体:
public Product GetProductById(int productId)
{
using (var db = new ProductContext())
{
var query = from pr in db.Products.Include("Picture")
where pr.Id == productId
select pr;
return query.FirstOrDefault();
}
}
I fixed the problem. 我解决了这个问题。 Not sure if this is the correct way, but it works. 不确定这是否是正确的方法,但它确实有效。
I did the following changes: 我做了以下更改:
public class Product
{
public int? PictureId { get; set; }
public int Id {get; set;}
public Picture Picture { get; set; }
public bool Enabled { get; set; }
public string Text { get; set; }
}
modelBuilder.Entity<Product>().HasOptional<Picture>(pr => pr.Picture).WithMany().HasForeignKey(pr => pr .PictureId);
public Product GetProductById(int productId)
{
var query = from pr in _productRepository.Table
where pr.Id == productId
select pr;
var product = query.FirstOrDefault();
AddProductPicture(product);
return product;
}
private void AddProductPicture(Product product)
{
var query = from pic in _pictureRepository.Table
where pic.Id == product.PictureId
select pic;
product.Picture = query.FirstOrDefault();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.