c# - Entity Framework 5.0 code first one to one and one to many relationship -
i have 2 entities- product , picture. thought product can have 1 image or none , image can mapped many products.
public class product { public int id {get; set;} public image picture { get; set; } public bool enabled { get; set; } public string text { get; set; } }
the picture entity mapping pictures entity contains of pictures (this image entity contains mapping pictures products), contains 2 columns ids.
public class image { public int id {get; set;} public int pictureid { get; set; } }
this model binder product entity
modelbuilder.entity<product>().hasoptional<picture>(pr => pr.picture);
when create product entity picture, or add together image existing entity picture_id column created in product table correctly populated id picture table. problem when seek retrieve product. picture entity within product entity null. suspect mapping incorrect. can tell me how populate picture entity within product entity when retrieve it. here retrieval code:
public product getproductbyid(int productid) { var query = pr in _productrepository.table pr.id == productid select pr; homecoming query.firstordefault(); }
edit: using custom irepository service include dependency injection
irepository<product>
the irepository interface contains next methods:
void insert(t entity); void update(t entity); void delete(t entity); iqueryable<t> table { get; }
this implementation of table:
public virtual iqueryable<t> table { { homecoming this.entities; } } private idbset<t> entities { { if (_entities == null) _entities = _context.set<t>(); homecoming _entities; } }
i can not add together .include("picture") table, because iqueryable. enabled lazy loading, there no result.
var products = db.pictures.include("product"); public product getproductbyid(int productid) { var query = pr in products pr.id == productid select pr; homecoming query.firstordefault(); }
c# entity-framework asp.net-mvc-4 entity-framework-5
No comments:
Post a Comment