【发布时间】:2021-11-05 15:34:59
【问题描述】:
我正在开发一个带有 MS-SQL 数据库的 ASP .NET Core 5 MVC 应用程序,而 EF Core 是我的 ORM(采用代码优先方法)。
目前我正在重新设计数据库,我有多个共享许多相同属性的表,并且我希望能够从不同的表中添加/删除属性,但只能在代码中的一个位置。
因为 .NET 没有多类继承,所以我想出了在“层级”中继承类的想法。 基本上,在下面的示例中,purchasing 表和 products 表应该具有完全相同的价格、日期和内容属性,但还有一些额外的特定字段:
class Purchase : PriceDatesAndContent
{
// specific purchase properties
}
class Product : PriceDatesAndContent
{
// specific product properties
}
class PricesDatesAndContent : PriceAndDates
{
public string Name { get; set ; }
public string Description { get; set; }
}
class PricesAndDates : Prices
{
public DateTime someDate1 { get; set; }
public DateTime someDate2 { get; set; }
// ...
}
class Prices
{
public double Price1 { get; set; }
public double Price2 { get; set; }
}
但是,我不确定这在我看来是否真的是一个绝妙的主意,我很想听听您的意见,或者您甚至有针对这种情况的其他解决方法?
非常感谢!
【问题讨论】:
标签: asp.net-mvc database entity-framework multiple-inheritance