1

So, following this example (https://msdn.microsoft.com/en-us/data/jj193542) I was able to manually type out the Entity classes and DbContext class in order to create a new database from scratch.

That is all well and good, but, I need to programmatically create the DbContext and Entities (from an assembly I built that provides the metadata) not manually type them out. I then need the database to be created in SQL on the fly during compile or run time, not when a record is programmatically added (as the example in the above link demonstrates).

Is this possible with the Code-First method?

3
  • So, using t4 templates I was able to include my assembly and generate the DbContext and Entity classes. The problem is I still need to programmatically open the DbContext, create a new empty entity and add it then call db.SaveChanges() in order for the database and tables to get created in SQL!! There has got to be another way to create the database!! Anyone? Commented Jun 28, 2016 at 19:24
  • 1
    Database.Initialize would create the database. msdn.microsoft.com/en-us/library/… Commented Jun 29, 2016 at 2:28
  • That did it, thank you sir. Commented Jun 29, 2016 at 18:07

1 Answer 1

0

This is now solved. To generate the DbContext and Entities I used T4 templates (for more on T4 templates see: https://msdn.microsoft.com/en-us/data/gg558520.aspx) and then simply call Database.Initialize(false) to force the database to create when the application (console application in this case) is executed.

        using (var db = new MyContext())
        {
            db.Database.Initialize(false);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.