如果我让每个类都从提供他的密钥/ID的基类继承,会不会有问题(或者可能是个坏主意)?这在实体框架中的代码优先设计中
class MyObject
{
[Key]
public int Id { get; set; }
}
class Person : MyObject {...}
class Message : MyObject {...}
class Whatever : MyObject {...}
我想这样做是因为发生了很多事情,我希望两个/三个类(例如)从某个基类继承,而我不负责提供 id 的那个基类。
具体例子,比较一下:(实际场景)
class Recipient
{
[Key]
public int RecipientId { get; set; } // I want to avoid this
public string DisplayName { get; set; }
}
class Person : Recipient {...}
class Group : Recipient {...}
为此:(我想要实现的目标)
class MyObject
{
[Key]
public int Id { get; set; }
}
class Recipient : MyObject
{
public string DisplayName { get; set; }
}
class Person : Recipient {...}
class Group : Recipient {...}