我在这里读到了这个问题:
有没有办法覆盖 LINQtoSQL 生成的类中的空构造函数?
通常我的构造函数看起来像:
public User(String username, String password, String email, DateTime birthday, Char gender)
{
this.Id = Guid.NewGuid();
this.DateCreated = this.DateModified = DateTime.Now;
this.Username = username;
this.Password = password;
this.Email = email;
this.Birthday = birthday;
this.Gender = gender;
}
但是,正如在该问题中所读到的,您希望使用部分方法 OnCreated() 来分配值而不是覆盖默认构造函数。好的,我得到了这个:
partial void OnCreated()
{
this.Id = Guid.NewGuid();
this.DateCreated = this.DateModified = DateTime.Now;
this.Username = username;
this.Password = password;
this.Email = email;
this.Birthday = birthday;
this.Gender = gender;
}
但是,这给了我两个错误:
Partial Methods must be declared private.
Partial Methods must have empty method bodies.
好的,我将其更改为Private Sub OnCreated()
以删除这两个错误。但是我仍然坚持......我怎样才能像使用普通的自定义构造函数一样传递它的值?我也在 VB 中执行此操作(转换它,因为我知道最了解/更喜欢 C#),那会对此有影响吗?