1

i'm using EntityTypeConfiguration to map my database.

The problem is, the class T_DOC_GENERIC inherits T_DOC, when I set my relationship WithMany he expects an object T_DOC_GENERIC which he's declared as 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);
        }
    }

Cannot implicitly convert type 'System.Collections.Generic.ICollection<Protocol.Models.BaseEntities.T_DOC>' to 'System.Collections.Generic.ICollection<Protocol.Models.BaseEntities.GenericsEntities.T_DOC_GENERIC>'. An explicit conversion exists (are you missing a cast?) D:\PortalProtocolo\Models\Mappers\GenericsMappers\T_DOC_GENERIC.cs

There's a way to cast inside the lambda expression?

I tried an explicit cast like .WithMany((T_DOC)tType => tType.T_DOC) but I have no ideia how!

Someone can help me?

Pedro
  • 25
  • 5
  • If it were to work, the cast would be placed AFTER the lambda. `.WithMany(ttType => (T_DOC)tType.T_DOC)` – Khan Oct 29 '14 at 19:42
  • I tried too, but still doesn't work! – Pedro Oct 29 '14 at 19:43
  • I don't think this would even compile? Is `T_DOC_GENERIC` supposed to be a typod constructor name? – Asad Saeeduddin Oct 29 '14 at 19:51
  • Asad, I changed the original name but I forgot to change the constructor too, I didn't see this! edited, thanks! – Pedro Oct 29 '14 at 19:54
  • Okay, so let's try to understand what you're trying to do in terms of database tables. You want a table named `T_DOC`, which will have a nullable foreign key pointing to the primary key of a `T_TYPE` table. The type you have mapped to the `T_DOC` table is named `T_DOC_GENERIC`, which is a bit confusing, but okay. Where does the `T_DOC` **type** come into this? Is `T_DOC` mapped to another table? Do you want a table per type hierarchy? – Asad Saeeduddin Oct 29 '14 at 19:58
  • Asad, The Relationship between the tables are 0.1 - *. My table T_DOC is the base class, I had to create T_DOC_GENERIC who inherits T_DOC because my APP access 4 databases, which they share almost the same fields. The differents fields I put in my generic class (I will create a connection factory to manage which database I'm gonna use). I have a table called T_TYPE which has a field of type T_DOC, so when I'm trying to build my relationship .WithMany(tType => tType.T_DOC), I'm passing a field of type T_DOC when he is expecting a T_DOC_GENERIC. – Pedro Oct 29 '14 at 20:27

1 Answers1

1

Write a converter to convert/map from T_DOC to T_DOC_GENERIC (return type) in the T_DOC class to perform this cast:

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;
}

You can add this to an existing class or make it static if you prefer.

grovesNL
  • 6,016
  • 2
  • 20
  • 32