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);
}
Entity framework is not able to work with interfaces. Your navigation properties must use the real entity types (mapped classes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With