Is there a way to override the empty constructor in a class generated by LINQtoSQL?

前端 未结 4 569
太阳男子
太阳男子 2021-01-17 17:49

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 t

4条回答
  •  长发绾君心
    2021-01-17 18:25

    The default constructor which is generated by the O/R-Designer, calls a partial function called OnCreated - 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 = "";
    }
    

    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.

提交回复
热议问题