2

I'm trying EF using Code-First approach but with existing database. After searching I found the important thing to do is set the Initializer for my context to null with SetInitializer<>. Now in most samples I found they declare MyDbContext constructor with static modifier. Check this SO answer.
So what's the point of declaring static constructor?

Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60

1 Answers1

3

Static constructors are automatically executed before any other constructors, and before any static members are accessed. They are only called once per appdomain and thus are useful to initialize statics such as calling the static SetInitializer method on the Database type.

Olav Nybø
  • 11,454
  • 8
  • 42
  • 34
  • So it's not **required** to use static constructer..it's just better way to do it? – ebram khalil Oct 27 '13 at 17:11
  • Right, it doesn't matter to Entity Framework how you call the SetInitializer, just make sure you call it before anything else on your context if you need to change the default initializer. If you don't need to change the default which is CreateDatabaseIfNotExists you don't need to call it at all. – Olav Nybø Oct 27 '13 at 17:36