【发布时间】:2015-12-22 12:57:48
【问题描述】:
我正在使用 Code first Method 开发 MVC 应用程序。我创建了一个名为 IEntityBase 的通用接口。我已经定义了将在所有表中使用的基本列。我想为某些列分配默认值,以便在创建新行时默认分配这些值。正如我们可以在每个类构造函数中定义 then 一样,但我想将它们分配为泛型,以便将来如果我要添加任何新列,那么我可以这样做。
我可以为此使用 IOC 吗?
例如
public interface IEntityBase
{
int ID { get; set; }
bool IsActive { get; set; }
bool IsDeleted { get; set; }
DateTime? Created { get; set; }
string CreatedBy { get; set; }
}
上面的接口是有5列的接口。我想将默认值分配给 IsActive 为 true,IsDeleted 为 False,Created 为今天的日期。
请帮我解决这个问题。
【问题讨论】:
-
尝试定义一个基类。
-
接口定义了一个契约,而不是一个实现。类定义实现。正如@Steven 所说,您需要使用每个事物都继承自的基类。使其抽象化以避免 EF 将其实现为 STI。
标签: c# asp.net-mvc class interface