实体框架:使用接口扩展部分类,而不触及EF生成的类

时间:2013-04-17 10:12:04

标签: c# interface entity-framework-5 partial-classes

我的问题类似于这篇文章: Interface inheritance in Entity Framework

因此,我复制了该示例并根据我的问题对其进行了修改:

public interface IBaseEntity
{
    DateTime CreatedOn { get; set; }
    string CreatedBy { get; set; }
}

// The following two classes are generated by Entity Framework
public partial class SomeEntity
{
    public int SomeEntityId { get; }
    public string Name { get; set; }
    public DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
}

public partial class OtherEntity
{
    public int OtherEntityId { get; }
    public float Amount { get; set; }
    public DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
}


我想做的是以下几点:

public partial class SomeEntity : IBaseEntity
    {
        // No code needed here because the other partial class
        // already has the needed properties
    }

但后来我收到以下错误:

Error 64 '[...].SomeEntity' does not implement interface member '[...]IBaseEntity.CreatedOn'
Error 64 '[...].SomeEntity' does not implement interface member '[...]IBaseEntity.CreatedBy'

显然我也可以看到。
但我认为在编译之后,部分类将成为一个,因此编译器不应该抱怨。

有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:-1)

我有同样的错误,我解决了移动App_Code文件夹外的部分类! 我应该承认我不明白原因: - (