6

I created some tables in an .edmx file and have been generating the database by selecting "Generate Database From Model" and manually executing an .edmx.sql file on the database to build the tables.

Now, however, I am creating a setup dialog that allows the user to connect the program up to their own database. I thought running context.CreateDatabase would be good enough to create the database, along with the tables, but the tables are not created.

What is the preferred method for creating the database and tables when the user specifies their own server and database to use, when originally starting with a model?

David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • This might be of some help: http://stackoverflow.com/questions/5701608/unique-key-with-ef-code-first/5701702#5701702 – Kyeotic Jul 06 '12 at 21:29
  • I think that's for code-first, rather than model-first. – David Sherret Jul 06 '12 at 21:53
  • 2
    It is correct to answer your own question and mark it answered. I don't know why @Mark Kram suggested you to delete the answer and modify the question. If you feel that your solution solved the problem undelete your answer. – Ladislav Mrnka Jul 09 '12 at 09:04
  • 5
    That's what I always thought about stackoverflow and I don't know why someone would downvote me for coming back here providing answer once I found it. I even just found this: http://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question – David Sherret Jul 09 '12 at 14:53

1 Answers1

7

Ok, I figured out a way of creating the tables and database (not necessarily the best way). The result of CreateDatabaseScript can be executed:

// create the database
db.CreateDatabase();
// grab the script to create the tables
string createScript = db.CreateDatabaseScript();
// execute the script on the database
db.ExecuteStoreCommand(createScript);

You have to do some checking to make sure the tables and database aren't created already, otherwise the application will crash.

I'm going to take a stab at switching my project over to code-first though. Hopefully the above will help anyone looking.

David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • 1
    You should think about editing your original post rather than answering your own question. – Mark Kram Jul 07 '12 at 02:15
  • 10
    @MarkKram, answering your own question is not only ok, but I'd say it's a recommended approach on SO. That way you can clearly distinguish unanswered questions that need more attention from those resolved ones. – walther Jul 09 '12 at 15:12
  • @walther you are aware of the fact that you are able to edit your original post. You could simply indicate the resolution to your question. It makes it a little easier for people searching to find the correct answer, just sayin'. – Mark Kram Jul 09 '12 at 15:46
  • 11
    @MarkKram, I'm aware of that, but maybe you're not aware of the fact that we're not talking about forum posts, but rather about editing a question on a Q&A site. Answer shouldn't be a part of the question because it usually confuses people. The goal of SO is to provide a simple mechanism - someone asks a question and someone provides the answer. If you join those two parts together, it suddenly gets really weird, especially when there are more answers in the thread. Which answer is the one? The one in the question? Or one of those from other people and OP was just lazy to accept? – walther Jul 09 '12 at 15:53
  • 1
    @walther ok, lets agree to disagree. – Mark Kram Jul 09 '12 at 16:15
  • Your answer needs more context. What type is `db` for instance. As it stands, this answer is of no use. – C.J. Apr 27 '17 at 15:04
  • @CJohnson true, it does, but disagree on the answer being of no use because people can still search for the methods used to deduce the class. `db` is the database context ([see here](https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.createdatabase(v=vs.110).aspx)). I'll update both the question and answer. – David Sherret Apr 27 '17 at 15:10
  • David: Even with the modification it's still no use. The result? The result of your code? I'm still at a loss what type db is? – C.J. Apr 27 '17 at 19:24
  • @CJohnson kind of hard to show tables being created... see my last comment for the type of db (it's also linked to in the question and answer now). Also, are you working with model-first entity framework or code-first? This is for model-first. The database context (`db`) should have all these methods. Perhaps this question and answer isn't applicable to your situation. – David Sherret Apr 27 '17 at 19:35