7

I am using the the latest version of Entity Framework (4.2) and trying to implement interfaces for my Entities and for some reason, it isn't compiling. it is throwing an error "Cannot convert expression type ICollection<IOrder> to return type ICollection<Order>". if I don't use interfaces for the entities, then I don't get this error.

I have a separate project for interfaces (for repositories and services etc) and I need to pass the EF entities in those methods as parameters and I don't want to pass the actual entities in them, because that will require the interface project to have a dependency on the EF entities.

my goal is somewhat similar to the one mentioned in this post Can I abstract Entity Framework away from my Entities?

here is the sample. I just put a sample here, my actual entities are different, but the problem is same.

public interface IOrder
{
    int OrderId { get; set; }
    int CustomerId { get; set; }
    ICustomer Customer { get; set; }
}

public class Order : IOrder
{
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    ICustomer Customer { get; set; }
}

public interface ICustomer
{
    int CustomerId { get; set; }
    ICollection<IOrder> Orders { get; set; }
}

public class Customer : ICustomer
{
    public int CustomerId { get; set; }
    ICollection<IOrder> Orders { get; set; }
}

public class OrderMap : EntityTypeConfiguration<Order>
{
    this.HasOptional(t => t.Customer)
    .WithMany(t => t.Orders) //error comes from this line
    .HasForeignKey(d => d.CustomerId);
}
1

2 Answers 2

8

Entity framework is not able to work with interfaces. Your navigation properties must use the real entity types (mapped classes).

Sign up to request clarification or add additional context in comments.

8 Comments

but I have a project which consists only interfaces for example lets say I have a "IOrderService" which has a method called "IEnumerable<IOrder> GetOrders(ICustomer customer)" (which will in turn call a repository to fetch data. pls don't try to find fault with this, it is just an example). I can't use the real entity type here because this project needs to have a reference to real entities which seems not the right approach i.e. interface project having a reference to the real type. it should be normally other way round.
in fact I have all the EF entities in a separate project from the Entity Framework project to utilise them as business objects without any dependency on persistence. all the mapping and the database context etc are in a separate project it has a reference to the entities project.
This is not dependency on persistence. This is just dependency between entities and EF demands it. Simply EF doesn't support it and I'm not aware of any workaround. Merge your interfaces and entities to single assembly and define navigation properties with real entity types.
thanks. I thought of doing the same after reading your reply. I will merge the entities and interfaces in one single project.
+1 I had this same issue a while back and went through a bit of pain until I realised exactly what Ladislav Mrnka has explained!
|
1

"You can add your own partial class files to specify the interfaces to be implemented - and to provide any actual implementation methods you need" - as suggested here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.