实体框架6:使用界面导航属性可能吗? [英] Entity Framework 6: Using interface as navigation properties possible?

查看:118
本文介绍了实体框架6:使用界面导航属性可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用接口,在EF6导航属性?
我已经找到相关主题EF4或更早的地方似乎没有可能;通常,似乎继承自那时以来已经提高了很多,但我还没有找到一种方法,使这一特定问题的工作尚未

is there any way to use interfaces as navigation properties in EF6? I've found related topics for EF4 or earlier where it didn't seem to be possible; generally, inheritance seems to have improved a lot since then, but I haven't found a way to make this specific problem work yet.

例如:

public interface IPerson
{
  string name { get; set; }
}

public class Man : IPerson { /* ... */ }
public class Woman : IPerson { /* ... */ }

public interface ICar
{
  IPerson driver { get; set; }
}

public class Car : ICar
{
  public virtual IPerson driver { get; set; }  // This won't map
}



这是可能以任何方式?如果没有,是什么了做到这一点的最好方法是什么?

Is this possible in any way? If not, what'd be an advisable way to do this?

由于目前我没有看到任何一个接口有一个设置能够属性,其类型一些其他的接口(ICAR的IPerson特性,例如),哪一种给我的印象是一个非常严重的设计限制吗?!

Because currently I don't see any way for an interface to have a set-able property whose type is some other interface (the IPerson property of ICar, for example), which kind of strikes me as a very serious design limitation?!

推荐答案

好吧,对于那些可能面临着同样的问题在未来。各地的测试之后,这是我现在怎么做。

Okay, for those possibly facing the same issue in the future. After more testing around, this is how I'm doing it now.

public interface IPerson
{
  string name { get; set; }
}

public abstract class APerson : IPerson
{
  public string name { get; set; }
}

public class Man : APerson { /* ... */ }
public class Woman : APerson { /* ... */ }

public interface ICar
{
  IPerson driver { get; set; }
}

public class Car : ICar
{
  // This maps to the database
  public virtual APerson driver { get; set; }

  // And this implements the interface
  ICar.driver
  {
    get
    {
      return (IPerson)driver;
    }
    set
    {
      if(!(value is APerson))
        throw new InvalidCastException("driver must inherit from APerson");

      driver = (APerson)value;
    }
  }
}

这变得更加棘手一点有当一个一对多/多对一一对多的关系,这种情况下我已经写了从收集和LT继承的类;接口类型>中,也实现了ICollection的<抽象基类>和再抛出一个异常,当有人尝试添加/设置不从抽象基类继承的任何对象。这基本上是一个集合< IPerson>有保证只包含继承继承APerson,如果你愿意的对象。

This gets a bit more tricky when having one-to-many / many-to-many relations, for that case I've written a class that inherits from Collection<Interface type>, but also implements ICollection<Abstract base type>, and again throws an exception when someone tries adding/setting any object that doesn't inherit from the abstract base class. It's basically a Collection<IPerson> that's guaranteed to only contain objects inheriting that inherit APerson, if you will.

这解决方案肯定是不理想的,因为它只是抛出一个异常,如果有人试图将一个值赋给司机不从APerson继承,因此在这里没有编译时的安全性。
,而这是我能想到的迄今为止最好的解决办法,如果你真的想保持你的接口分开,自成一体。

This solution is definitely not ideal, because it just throws an exception if somebody tries assigning a value to driver that does not inherit from APerson, so no compile-time safety here. But it's the best solution I could think of so far, if you really want to keep your interfaces separate and self-contained.

这篇关于实体框架6:使用界面导航属性可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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