31

I am reading this book, and it tries to use initializer to Create the DB each time the application runs, so the code snippet is like this:

protected void Application_Start() { Database.SetInitializer(new DropCreateDatabaseAlways<MusicStoreDB>()); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } 

I can't understand this part:

 new DropCreateDatabaseAlways<MusicStoreDB>() 

What is this syntax? what does <MusicStoreDB>() mean?

I know it's not a fancy question, but I need help here.

Thanks.

4
  • 1
    It's CodeFirst it means recreate the database whenever the application starts, MusicStoreDB is the database Commented Jul 25, 2015 at 7:35
  • Hi toby, I know it's code first, I don't understand the C# syntax itself. Commented Jul 25, 2015 at 7:49
  • If you do not understand C# syntax then it is probably better to start with some introductory book on that language before diving into MVC framework Commented Jul 25, 2015 at 7:53
  • 1
    I understand Generics, but the usage here had me confused, Thanks for others who clarified it in the answers. Commented Jul 25, 2015 at 7:55

2 Answers 2

45

That syntax is called generics. In a nutshell (a very tiny nutshell), imagine that your app had more than 1 database (e.g. MusicStoreDB, MovieStoreDB, etc), you could use the same DropCreateDatabaseAlways class with the different db types. In other words, generics let you define classes and functions that can act on many different types, for example

List<int>, List<string>, List<MyAwesomeClass>

Sign up to request clarification or add additional context in comments.

1 Comment

Oh ok :) Thanks for explaining .. I hope others would try to help as you do instead of voting questions down , much appreciated :)
1

DropCreateDatabaseAlways is the database intializer base class. MusicStoreDB is the database which will be dropped and re-created everytime the application starts. DropCreateDatabaseAlways<MusicStoreDB>() is the code that does that

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.