有没有一种方法来覆盖由LINQtoSQL生成的类空构造? [英] Is there a way to override the empty constructor in a class generated by LINQtoSQL?
问题描述
如果我在我的数据库中的表名为用户,也将是LINQtoSQL产生一类名为用户与已经声明空的构造函数。
If I have a table in my database called 'Users', there will be a class generated by LINQtoSQL called 'User' with an already declared empty constructor.
什么是最好的做法,如果我要重写此构造并加入我自己的逻辑呢?
What is the best practice if I want to override this constructor and add my own logic to it?
推荐答案
这是由O / R设计器生成的默认构造函数,调用了部分函数调用的OnCreate - 所以最好的做法是不是要覆盖默认的构造函数,而是实现了部分功能OnCreated在MyDataClasses.cs初始化项目:
The default constructor which is generated by the O/R-Designer, calls a partial function called OnCreate - so the best practice is not to override the default constructor, but instead implement the partial function OnCreated in MyDataClasses.cs to initialize items:
partial void OnCreated()
{
Name = "";
}
如果要实现其他的构造,总是小心地调用默认的构造函数,以便类将进行正确的初始化 - 例如entitysets(关系)的默认构造函数构造
If you are implementing other constructors, always take care to call the default constructor so the classes will be initialized properly - for example entitysets (relations) are constructed in the default constructor.
这篇关于有没有一种方法来覆盖由LINQtoSQL生成的类空构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!