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

时间:2009-02-07 07:51:09

标签: vb.net entity-framework domain-driven-design

示例界面:

public Interface IEntity
     Property ID() as Integer
end Interface

我希望所有EF对象都在主键上实现此接口。

这可能吗?

3 个答案:

答案 0 :(得分:2)

这在CSharp中看起来很容易,但在VB中你必须明确声明哪些属性/功能/子实现接口:

public Property Id() as Integer Implements IEntity.Id

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

答案 1 :(得分:1)

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

/* 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
{
}

答案 2 :(得分:0)

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