代码之家  ›  专栏  ›  技术社区  ›  Kelly

是否可以强制实体框架生成的属性实现接口?

  •  1
  • Kelly  · 技术社区  · 17 年前

    接口示例:

    public Interface IEntity
         Property ID() as Integer
    end Interface
    

    我希望我的所有ef对象在那里实现这个接口主键。

    这有可能吗?

    3 回复  |  直到 17 年前
        1
  •  2
  •   Kelly    17 年前

    这在CSharp中似乎很容易做到,但在VB中,您必须明确声明哪些属性/函数/子函数正在实现接口:

    public Property Id() as Integer Implements IEntity.Id
    

    不幸的是,我不得不撕掉设计器文件并修改生成的属性。我最终把生成的文件全部删除了,现在将我的模型保存在具有所有属性映射的单独类中。

        2
  •  1
  •   Jeroen Huinink    17 年前

    是的,你可以。设计器生成的类被声明为分部类。在单独的源文件中,您可以为这些类声明其他方法。还可以声明已由生成的类实现的特定接口。

    /* This is the interface that you want to have implemented. */
    public interface ISomething
    {
        void DoSomething();
    }
    
    /* This would be part of the generated class */
    partial class PartialClass
    {
        public void DoSomething()
        {
        }
    }
    
    /* This would be your own extension */
    partial class PartialClass : ISomething
    {
    }
    
        3
  •  0
  •   Frans Bouma    17 年前

    这些类是部分的,所以应该很容易做到。