【发布时间】:2021-03-06 16:35:33
【问题描述】:
我是 C# 新手,我正在遵循课程说明,并创建了一个自定义构造函数来验证数据。
但我想知道:我们为什么要为我们的类创建一个默认构造函数?
这是我的代码:
public class UserNotification
{
[Key]
[Column(Order =1)]
public string UserId { get; set; }
[Column(Order = 2)]
[Key]
public int NotificationId { get; set; }
public ApplicationUser User { get; set; }
public Notification Notification { get; set; }
public bool IsRead { get; set; }
protected UserNotification(){ }
public UserNotification(ApplicationUser user, Notification notification)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
if (notification == null)
{
throw new ArgumentNullException("notification");
}
Notification = notification;
User = user;
}
}
【问题讨论】:
-
因为查询数据库时使用了默认构造函数,并且对象被物化了。查询 db 时,EF 应该将哪些值传递给构造函数?
标签: c# entity-framework oop constructor