none
Code First... Should we always call the base after overriding Seed and OnModelCreating? RRS feed

  • Question

  • When overriding OnModelCreating, should we always call base.OnModelCreating in our method?

    I see a lot of sample code out there that doesn't...

    		protected override void OnModelCreating(DbModelBuilder modelBuilder)
    		{
    			modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    			base.OnModelCreating(modelBuilder);
    		}

    Is it the same for the Seed method of the Initializers:

    	public class ServerContextInitializer : DropCreateDatabaseAlways<ServerContext>
    	{
    		protected override void Seed(ServerContext context)
    		{
    			
    			base.Seed(context);
    		}
    	}
    

    Wednesday, January 18, 2012 12:17 PM

Answers

  • Hi Yvangelist;

    To your question, "When overriding OnModelCreating, should we always call base.OnModelCreating in our method?", The Microsoft documentation states the following, "The default implementation of this method does nothing, but it can be overridden in a derived class such that the model can be further configured before it is locked down", and some of the books I read do not show it.

    The same is true for DropCreateDatabaseIfModelChanges<TContext>.Seed Method

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Yvangelist Thursday, January 19, 2012 2:13 AM
    Wednesday, January 18, 2012 9:55 PM
  • Hi,

    At the moment it does not really matter. Both OnModelCreating and Seed methods don't have any implementation (i.e. the bodies of the functions are empty) in their base classes. Therefore calling into the base class is basically a no-op. I don't think there are any plans to change it (especially for Seed method - how we would know what data to put in the database?)

    In general protected methods can have some implementation in base classes. In that case this is up to the developer who derives from such a class to decide whether s/he would like to take advantage of the functionality provided in the method in the base class or not. If the developer thinks that s/he only would like to add something to the functionality provided by the method in the base class s/he would call into this method. Otherwise s/he would not call into the method of the base class and would provide all the required functionality in the method on the derived class.

    Hope this helps.

    Pawel

    • Marked as answer by Yvangelist Thursday, January 19, 2012 2:13 AM
    Wednesday, January 18, 2012 9:53 PM

All replies

  • Hi,

    At the moment it does not really matter. Both OnModelCreating and Seed methods don't have any implementation (i.e. the bodies of the functions are empty) in their base classes. Therefore calling into the base class is basically a no-op. I don't think there are any plans to change it (especially for Seed method - how we would know what data to put in the database?)

    In general protected methods can have some implementation in base classes. In that case this is up to the developer who derives from such a class to decide whether s/he would like to take advantage of the functionality provided in the method in the base class or not. If the developer thinks that s/he only would like to add something to the functionality provided by the method in the base class s/he would call into this method. Otherwise s/he would not call into the method of the base class and would provide all the required functionality in the method on the derived class.

    Hope this helps.

    Pawel

    • Marked as answer by Yvangelist Thursday, January 19, 2012 2:13 AM
    Wednesday, January 18, 2012 9:53 PM
  • Hi Yvangelist;

    To your question, "When overriding OnModelCreating, should we always call base.OnModelCreating in our method?", The Microsoft documentation states the following, "The default implementation of this method does nothing, but it can be overridden in a derived class such that the model can be further configured before it is locked down", and some of the books I read do not show it.

    The same is true for DropCreateDatabaseIfModelChanges<TContext>.Seed Method

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Yvangelist Thursday, January 19, 2012 2:13 AM
    Wednesday, January 18, 2012 9:55 PM
  • Thanks Pawel. Sounds good.
    Thursday, January 19, 2012 2:15 AM
  • Thanks Fernando. Sounds like I should have read more of the documentation instead of spending my time in the tutorials! ;-)

     

    Thursday, January 19, 2012 2:17 AM
  •  

    Not a problem, glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, January 19, 2012 3:34 PM
  • Thanks, Do it equally applies on EF core?

    Sunday, January 6, 2019 4:20 PM