Code First - Entity Framework

Discussion in 'Databases' started by coopervegas, Mar 28, 2012.

  1. mvc 3 - basic db connection

    Hi, I'm new to web developement. I looked on the forum couldnt find what I'm looking for, appreciate a boost in the right direction.

    I'm currently reading an mvc3 book following along with the musicstore application.

    Basically all I want to test is making a super basic application that connects to a db on my website and reads a table; created with the entity framework like the book does.

    Am I assuming correctly everything I need to do is just like the book, except I need to figure out my connectionString options?

    the example has: (web.config file)

    connectionString="data source=.\SQLEXPRESS;
    Integrated Security=SSPI;
    initial catalog=MusicStore"

    What would I have to change this this variable to get it working?
    And maybe I am wrong and would have to create the database first on the website to get it working, vs. letting the mvc code do it for me?

    Thanks for any help and/or direction to some post called 'idiots guide to using a database with mvc3.

    Or even if there is a simple project in existance i could use as a template or if someone could make one; once i get it very basics up im sure ill be on my merry way building it out, thanks.
  2. Well I answered most of my own question I think.. when I created a database using Winhosts control panel it gave me all the info I needed with the connectionString.

    I guess my concern that remains is the code-first database creation going to work; I guess trial and error will tell me.
    Last edited by a moderator: Oct 14, 2015
  3. Hi,

    I'm following the musicstore sample project in 'professional mvc3' and got it working locally.

    Trying to run it on my website I cant seem to figure out the magic changes I need to do. Looks like a key problem is you cant create a database in code which it does.

    I saw a post where someone changed their seed code to inherit from

    public class SampleData : CreateDatabaseIfNotExists<MusicStoreEntities>

    instead of the default

    //public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>

    The error I'm getting now is :

    Invalid object name 'dbo.Genres'.

    This is the first table that the codefirst seed code is supposed to make.

    What do I need to change to get the code-first entity framework going?
    Or am I going to need to manually create the database and all the tables first?

    Hopefully I'm not the first one to try to do this and there is a thread or something I can look at or an easy 'oh do this few lines of code' to get it up and running, thanks.
  4. Or thinking about it more is the real answer....

    "do the code first in development on your local db, when you upload the project to the website create the database first".

    Any help or direction would be great, thanks.
  5. code first is a development method. it is used to create a database from your code. You know all of this so sorry about the babble.

    At some point you have a usable database that is ready for production. At this point it is time to perhaps make a backup of the database upload to your account and create a database on your hosting account and restore it with the backup of the one that is no longer in development.
    Or perhaps scripting it and runing the script against an empty database located in you hosting enviroment.

    Code first is a great tool and in a team enviroment it may be done using a shared database that is set up that allows the drop and create but most hosting companies do not offer that ability.

Share This Page