简体   繁体   English

我可以使用并返回EF4代码优先的POCO实体作为其接口吗?

[英]Can I use and return EF4 code-first POCO entities as their interface?

Given the code later in the question, I am getting the following error from the EF4 code-first API: 鉴于问题后面的代码,我从EF4代码优先API获得以下错误:

The given property 'Roles' is not a supported navigation property. 给定属性“角色”不是受支持的导航属性。 The property element type 'IRole' is not a supported entity type. 属性元素类型“IRole”不是受支持的实体类型。 Interface types are not supported. 不支持接口类型。

Basically, I have a Repository similar to the following: 基本上,我有一个类似于以下的存储库:

public class Repository : IRepository {
    private IEntityProvider _provider;
    public Repository(IEntityProvider provider) {
        _provider = provider;
    }
    public IUser GetUser(int id) {
        return _provider.FindUser(id);
    }
}

Notice that the IRepository.GetUser returns an IUser. 请注意,IRepository.GetUser返回一个IUser。

Let's say my IEntityProvider implementation looks like this. 假设我的IEntityProvider实现看起来像这样。

public class EntityProvider : IEntityProvider {
    public IUser FindUser(int id) {
        /* Using Entity Framework */
        IUser entity;
        using (var ctx = new MyDbContext()) {
            entity = (from n in ctx.Users 
                  where n.Id == id 
                  select (IUser)n).FirstOrDefault();
        }
        return entity;
    }
}

The key here is that the IUser interface has a List<IRole> property called Roles. 这里的关键是IUser接口有一个名为Roles的List <IRole>属性。 Because of this, it seems, the Entity Framework code-first cannot figure out what class to use to fulfill the IRole interface that property needs. 因此,似乎实体框架代码首先无法弄清楚用于实现属性所需的IRole接口的类。

Below are the interfaces and POCO entities which would be used throughout the system and hopefully also used with EF4. 下面是将在整个系统中使用的接口和POCO实体,并且希望也可以与EF4一起使用。

public interface IUser {
    int Id { get; set; }
    string Name { get; set; }
    List<IRole> Roles { get; set; }
}

public interface IRole {
    int Id { get; set; }
    string Name { get; set; }
}

public class User : IUser {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<IRole> Roles { get; set; }
}

public class Role : IRole {
    public int Id { get; set; }
    public string Name { get; set; }
}

Am I going about this the wrong way? 我是以错误的方式来做这件事的吗? Is there a way to do this within the EF4 code-first API? 有没有办法在EF4代码优先API中执行此操作?

I can only think of the following: 我只能想到以下几点:

  1. Some sort of shadow property (List<Role> DbRoles) that is used by EF4 code-first. EF4代码优先使用的某种shadow属性(List <Role> DbRoles)。 Then use Data Annotations to make sure the actual List<IRole> is ignored by EF4. 然后使用Data Annotations确保EF4忽略实际的List <IRole>。
  2. Create duplicate classes for all entities which EF4 code-first will use and then Map those to the official ones that implement the interface. 为EF4代码优先使用的所有实体创建重复的类,然后将它们映射到实现该接口的官方实体。

请记住,你需要使基类抽象化(用EF文档检查继承),我建议在其中没有任何内容的RootEntity,然后是一个带有一些常见信息的Base实体,如Id,InsertedBy,UpdatedBy like standart字段,它使一切变得更容易。

The use of Interfaces is not supported in EF 4 Code First (as of CTP5) and more than likely wont be supported in the RTM either. EF 4 Code First(从CTP5开始)不支持使用接口,并且RTM中也可能不支持使用接口。 I would say make an abstract class in your DbContext to hold your objects. 我想说在你的DbContext中创建一个抽象类来保存你的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM