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);
}
Community
  • 1
  • 1
RKP
  • 5,285
  • 22
  • 70
  • 111
  • possible workaround at http://stackoverflow.com/questions/9805329/how-to-use-interface-properties-with-codefirst/10994290#10994290 – Cel Jun 12 '12 at 10:22

2 Answers2

8

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

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • but I have a project which consists only interfaces for example lets say I have a "IOrderService" which has a method called "IEnumerable 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. – RKP Jan 05 '12 at 19:05
  • 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. – RKP Jan 05 '12 at 19:06
  • 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. – Ladislav Mrnka Jan 05 '12 at 19:11
  • thanks. I thought of doing the same after reading your reply. I will merge the entities and interfaces in one single project. – RKP Jan 05 '12 at 22:19
  • +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! – Tom Chantler Jan 07 '12 at 23:14
  • so, can EF work with Interfaces when you don't use any navigation properties? – Maslow Feb 23 '12 at 22:43
  • @RKP This might help: http://stackoverflow.com/questions/9805329/how-to-use-interface-properties-with-codefirst – Chris Moschini Aug 20 '12 at 04:02
  • The statement provided in the answer is correct however you can still implement interfaces in your model. See the answer here: http://stackoverflow.com/questions/25385161/entity-framework-6-using-interface-as-navigation-properties-possible/25427142#comment60373091_25427142 –  Apr 02 '16 at 20:04
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

Community
  • 1
  • 1
Colin
  • 22,328
  • 17
  • 103
  • 197