使用实体框架(4.2)实体的接口时出错 [英] error when using interfaces for Entity Framework (4.2) entities

查看:105
本文介绍了使用实体框架(4.2)实体的接口时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本的Entity Framework(4.2),并试图为我的实体实现接口,由于某些原因,它不是编译。它抛出一个错误无法将表达式类型ICollection< IOrder>转换为返回类型ICollection< Order> 。如果我不使用接口的实体,那么我不会得到这个错误。



我有一个单独的接口项目(用于存储库和服务等),我需要将这些方法中的EF实体作为参数传递,我不想通过实际的实体,因为这将需要接口项目对EF实体有依赖。



我的目标有点类似于这篇文章中提到的那个<一个href =https://stackoverflow.com/questions/694761/can-i-abstract-entity-framework-away-from-my-entities>我可以从实体抽象实体框架吗?



这里是样本。我只是把一个样本放在这里,我的实体是不同的,但问题是一样的。

  public interface IOrder 
{
int OrderId {get;组; }
int CustomerId {get;组; }
客户客户{get;组; }
}

public class Order:IOrder
{
public int OrderId {get;组; }
public int CustomerId {get;组; }
客户客户{get;组;
}

public interface ICustomer
{
int CustomerId {get;组; }
ICollection< IOrder>订单{get;组; }
}

public class Customer:ICustomer
{
public int CustomerId {get;组; }
ICollection< IOrder>订单{get;组; }
}

public class OrderMap:EntityTypeConfiguration< Order>
{
this.HasOptional(t => t.Customer)
.WithMany(t => t.Orders)//错误来自此行
.HasForeignKey( d => d.CustomerId);
}


解决方案

实体框架无法使用界面。您的导航属性必须使用真实实体类型(映射类)。


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).

这篇关于使用实体框架(4.2)实体的接口时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆