【发布时间】:2013-03-13 10:33:51
【问题描述】:
我有一个在运行时转换 DTO(产品)的 ProductViewModel。
public class ProductViewModel : IViewModel {
public ProductViewModel() {
Categories = new List<CategoryViewModel>();
}
#region DTO Helpers
public ProductViewModel(Product p) {
this.ID = p.ID;
this.Name = p.Name;
this.Price = p.Price;
Categories = new List<CategoryViewModel>();
}
#endregion
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public IEnumerable<CategoryViewModel> Categories { get; set; }
}
我之前在 LINQ2SQL 中使用过这段代码并且可以正常工作,但现在使用实体框架就不行了:
var products = (from p in db.GetAll()
select new ProductViewModel(p));
我收到此错误:
Only parameterless constructors and initializers are supported in LINQ to Entities
谁能帮忙解释/解决这个问题?
【问题讨论】:
标签: asp.net-mvc entity-framework linq-to-entities