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

前端 未结 4 466
太阳男子
太阳男子 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.

    0 讨论(0)
  • 2021-01-17 18:31

    It doesn't look like you can override the empty constructor. Instead, I would create a method that performs the functionality that you need in the empty constructor and returns the new object.

    // Add new partial class to extend functionality
    public partial class User {
    
      // Add additional constructor
      public User(int id) {
        ID = id;
      }
    
      // Add static method to initialize new object
      public User GetNewUser() {
        // functionality
        User user = new User();
        user.Name = "NewName";
        return user;
      }
    }
    

    Then elsewhere in your code, instead of using the default empty constructor, do one of the following:

    User user1 = new User(1);
    User user2 = User.GetNewUser();
    
    0 讨论(0)
  • 2021-01-17 18:34

    Setting DataContext Connection property to 'None' worked for me. Steps below.

    Open the dbml -> Right Click Properties -> Update Connection in DataContext properties to 'None'. This will remove the empty constructor from the generated code file. -> Create a new partial class for the DataContext with an empty constructor like below

    Partial Class MyDataContext    
        Public Sub New()             
            MyBase.New(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString, mappingSource)
            OnCreated()    
        End Sub    
    End Class
    
    0 讨论(0)
  • 2021-01-17 18:38

    Here's the C# version:

    public partial class PENCILS_LinqToSql_DataClassesDataContext
    {
         public PENCILS_LinqToSql_DataClassesDataContext() : base(ConnectionString(), mappingSource)
        {
        }
    
        public static String ConnectionString()
        {
            String CS;
            String Key;
    
            Key = System.Configuration.ConfigurationManager.AppSettings["DefaultConnectionString"].ToString();
    
            /// Get the actual connection string.
            CS = System.Configuration.ConfigurationManager.ConnectionStrings[Key].ToString();
    
            return CS;
        }
    }
    
    0 讨论(0)
提交回复
热议问题