【问题标题】:Is it Possible to Force Properties Generated by Entity Framework to implement Interfaces?是否可以强制实体框架生成的属性来实现接口?
【发布时间】:2009-02-07 07:51:08
【问题描述】:

示例界面:

public Interface IEntity
     Property ID() as Integer
end Interface

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

这可能吗?

【问题讨论】:

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


    【解决方案1】:

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

    public Property Id() as Integer Implements IEntity.Id
    

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

    【讨论】:

    • 注意,由于缺少属性映射文档,我通过 EF 框架切换到 Linq
    【解决方案2】:

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

    /* 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】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 2015-02-14
        相关资源
        最近更新 更多