【发布时间】:2013-10-06 01:36:27
【问题描述】:
我有以下抽象类和接口:
public interface ITypedEntity{
TypedEntity Type { get; set; }
}
public abstract class TypedEntity:INamedEntity{
public abstract int Id{get;set;}
public abstract string Name { get; set; }
}
但是当我尝试创建一个实现 ITypedEntity 并具有 TypedEntity 的具体实现的类时,我收到以下错误
Magazine does not implement interface member ITypedEntity.Type. Magazine.Type cannot implement 'ITypedEntity.Type' because it does not have the matching return type of 'TypedEntity'
我的具体实现代码如下
public class Magazine:INamedEntity,ITypedEntity
{
public int Id {get;set;}
[MaxLength(250)]
public string Name {get;set;}
public MagazineType Type { get; set; }
}
public class MagazineType : TypedEntity
{
override public int Id { get; set; }
override public string Name { get; set; }
}
我想我明白发生了什么,但我不知道为什么,因为 MagazineType 是一个 TypedEntity。
谢谢。
更新 1 我不得不提一下,我想将这些类与 EF CodeFirst 一起使用,并且我想为每个实现 ITypedEntity 的类创建一个不同的表。
【问题讨论】:
-
MagazineType 是 TypedEntity 但 TypedEntity 不是 MagazineType。也许你可以定义你的界面,比如
ITypedEntity Type { get; set; } -
但是引用
Magazine的代码存储在ITypedEntity类型的变量中可能会尝试将 不是MagazineType的内容存储到 @ 987654328@财产。 -
将
Magazine.Type的返回类型改为EntityType。它仍然可以是MagazineType的实例,但签名必须匹配以满足接口实现。
标签: c# asp.net entity-framework oop