c# – ExpressMapper / EntityFramework – 没有为此对象定义的无参数构造函数

我正在尝试使用ExpressMapper将数据实体映射到模型.

如果我直接将实体映射到模型(它们都具有相同的属性),那么它工作正常.

但是,如果我将链接的实体映射到模型,那么我会收到错误

There was an error: System.MissingMethodException: No
parameterless constructor defined for this object.

数据库结构:

《c# – ExpressMapper / EntityFramework – 没有为此对象定义的无参数构造函数》

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>>();

任何帮助将不胜感激.

最佳答案 我很欣赏这是一个非常古老的问题,但考虑到我只花了4个小时调试一个类似的错误:System.MissingMethodException:在ExpressMapper上没有为此对象错误定义无参数构造函数,我想我会对我的发现感兴趣.

所以我们遇到了类似于你的情况,因为我们有像这样的域模型(以下所有内容都是简化示例):

public class Owner 
{
    public int? ID { get; set; }
    public string Name { get; set; }
}

public class Animal
{
    public int? ID { get; set; }
    public string Name { get; set; }
    public int? OwnerID { get; set; }
    [ForeignKey("OwnerID")]
    public Owner Owner { get; set; }
}

使用以下视图模型(即我们的API发送和接收的内容):

public class AnimalViewModel
{
    public int? ID { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
}

像这样的映射:

Mapper.Register<Animal, AnimalViewModel>();
Mapper.Register<AnimalViewModel, Animal>();

在映射到域模型和视图模型之间或者从映射模型映射时,我们会得到MissingMethodException,尽管视图模型和域模型都有公共的默认构造函数.解决方案是手动映射域模型中的相关实体,并将它们从ExpressMapper的映射中排除,如下所示:

Mapper.Register<Animal, AnimalViewModel>()
    .Ignore(a => a.Owner);
Mapper.Register<AnimalViewModel, Animal>()
    .Ignore(a => a.Owner);

从阅读EntityMapper的源代码来看,似乎MissingMethodException是一个与实际问题无关的全红鲱鱼.实际问题似乎是它无法弄清楚如何将一种类型转换为另一种类型.在我们的例子中 – 复杂对象如上所述映射到基元/从基元映射 – 从映射器中排除相关对象并手动完成就足够了.

编辑:

经过进一步调查,我们在我们的案例中追溯了根本问题,回到EF代理创建创建生成的类型(例如’MyModel_14289012′)这一事实,这些类型与映射器中注册的类型不匹配.要防止这种情况,请将以下内容应用于上下文:

Context.Configuration.LazyLoadingEnabled = false;
Context.Configuration.ProxyCreationEnabled = false;

并手动包含模型中所需的任何嵌套/相关对象,如下所示:

Context.Animals
    .Include(a => a.Owner);

这将获取相关实体,但作为其实际类型而不是EF生成的类型.

点赞