1
我試圖使用Expressmapper將數據實體映射到模型。ExpressMapper/EntityFreamework - 沒有爲此對象定義的無參數構造函數
如果我將實體直接映射到模型(它們都具有相同的屬性),那麼它工作正常。
但是,如果我的地圖鏈接的實體模型,然後我得到一個錯誤
There was an error: System.MissingMethodException: No
parameterless constructor defined for this object.
數據庫結構:
ExpressMapper報名:
Mapper.Register<DiscountDaysOfWeek, DiscountDaysOfWeekModel>()
.Member(dest => dest.DiscountDayId, src => src.DiscountDayId)
.Member(dest => dest.DiscountDaysOfWeekId, src => src.DiscountDaysOfWeekId)
.Member(dest => dest.DiscountId, src => src.DiscountId)
.Member(dest => dest.Discountday, src => src.DiscountDay.Day);
調用這樣:
var disDays = discs.SelectMany(x => x.DiscountDaysOfWeeks)
.Map<IQueryable<DiscountDaysOfWeek>, IQueryable<DiscountDaysOfWeekModel>>();
在調用時獲取錯誤消息。
DiscountDaysOfWeekModel:
public class DiscountDaysOfWeekModel
{
public int DiscountDaysOfWeekId { get; set; }
public int DiscountId { get; set; }
public int DiscountDayId { get; set; }
public string Discountday { get; set; }
}
DiscountDayOfWeek(由EF產生)
public partial class DiscountDaysOfWeek
{
public int DiscountDaysOfWeekId { get; set; }
public int DiscountId { get; set; }
public int DiscountDayId { get; set; }
public virtual DiscountDay DiscountDay { get; set; }
public virtual Discount Discount { get; set; }
}
DiscountDay(由EF生成):
public partial class DiscountDay
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DiscountDay()
{
this.DiscountDaysOfWeeks = new HashSet<DiscountDaysOfWeek>();
}
public int DiscountDayId { get; set; }
public string Day { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DiscountDaysOfWeek> DiscountDaysOfWeeks { get; set; }
}
樣品工作之一:在下面的工作樣品的模型和實體具有相同的屬性
Mapper.Register<DiscountPreventedPriceEnding, DiscountPreventedPriceEndingModel>();
var execPriceEndings = discs.SelectMany(x => x.DiscountPreventedPriceEndings)
.Map<IQueryable<DiscountPreventedPriceEnding>, IQueryable<DiscountPreventedPriceEndingModel>>();
任何幫助將不勝感激。
謝謝!
這條線不能工作.Map,IQueryable >(); 告訴我一個例子,如果你有,你可以映射IQueryable。你可以把它改成ICollection嗎? –
DiscountDaysOfWeekModel是否具有無參數構造函數?顯示涉及的類。 –
@SteveGreene您好Steve,我添加了類 – Rocky