我在这里读了这个问题:LINQtoSQL自定义构造函数关闭部分类?
Is there a way to override the empty constructor in a class generated by 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#),那么这会对此产生影响吗?
啊好吧我没有想到User.New(),我试图做我自己的构造函数,但后来当我试图保存该对象失败,因为它没有包含很多自动生成的类的东西需要默认的构造函数提供。谢谢! – SventoryMang 2010-04-16 03:01:14
我只是想添加'User.New()'不起作用,您会收到以下错误:构造函数调用仅作为实例构造函数中的第一条语句有效。相反,似乎工作的是'Me.New()' – SventoryMang 2010-04-25 23:06:57
对不起 - 我不是很了解VB。希望你相当快地到达Me.New()。 – 2010-04-26 18:32:24