1

我正在使用 EntityTypeConfiguration 来映射我的数据库。

问题是,类 T_DOC_GENERIC 继承了 T_DOC,当我设置我的 WithMany 关系时,他期望一个对象 T_DOC_GENERIC,他将其声明为 T_DOC。

public class T_DOC_GENERICMapper : EntityTypeConfiguration<T_DOC_GENERIC>
    {
        T_DOC_GENERICMapper()
        { 
            this.ToTable("T_DOC");
            this.HasKey(tDoc => tDoc.ID);
            this.HasOptional(tDoc => tDoc.T_TYPE)
                .WithMany(tType =>  tType.T_DOC)
                .HasForeignKey(tDoc => tDoc.COD_TYPE);
        }
    }

无法将类型“System.Collections.Generic.ICollection<Protocol.Models.BaseEntities.T_DOC>”隐式转换为“System.Collections.Generic.ICollection<Protocol.Models.BaseEntities.GenericsEntities.T_DOC_GENERIC>”。存在显式转换(您是否缺少演员表?) D:\PortalProtocolo\Models\Mappers\GenericsMappers\T_DOC_GENERIC.cs

有一种方法可以在 lambda 表达式中进行转换吗?

我尝试了像 .WithMany((T_DOC)tType => tType.T_DOC) 这样的显式转换,但我不知道怎么做!

有人可以帮助我吗?

4

1 回答 1

1

编写一个转换器以在类中转换/映射 from T_DOCto T_DOC_GENERIC(返回类型)T_DOC以执行此转换:

public T_DOC_GENERIC ConvertToGeneric(T_DOC source)
{
    T_DOC_GENERIC destination = new T_DOC_GENERIC(){};

    /* Map T_DOC source to T_DOC_GENERIC destination here */

    return T_DOC_GENERIC;
}

您可以将其添加到现有类中,或者如果您愿意,可以将其设为静态。

于 2014-10-29T23:10:29.600 回答