2

I am new to ASP.net's MVC 3 (using VB) and I am trying to create an application that would connect to an SQL 2008 server database. I have gone through a tutorial (Microsoft ASP.net's Movie DB tutorial) but the tutorial uses SQL compact edition. I am having a hard time connecting. Am I correct in assuming that once I create a model, I should be able to just connect to SQL 2008 by changing the connection string in the Web.config file, found at the root of the application folder? I deleted the sql ce database from the App_Data folder. In Microsoft SQL Server Management Studio I created a new database. I then added this to my Web.config file:

      <connectionStrings>
<add name="ConnectionName"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=DELERIUM-PC;Initial Catalog=iDjItDb;Integrated         Security=True" />
       </connectionStrings>

The when I run the app, and try to view the controller associated with the model, i get this error:

The model backing the 'iDjItDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

What must I do to connect and work with a 2008 SQL database?

Thanks jason

1 Answer 1

3

You can remove the IncludeMetadataConvention in your context class if you are positive that your model is compatible with the database.

public class iDjItDBContext : DBContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    }
}

Otherwise you need to set the initializer in the Application_Start() method of your Global.asax.cs file.

Database.SetInitializer<iDjItDBContext>(
        new DropCreateDatabaseIfModelChanges<iDjItDBContext>());

Otherwise you can take the Migrations option where an external tool will generate the change script.

Edit Change the connection string name to iDjItDBContext so that the name matches with the DbContext name.

<connectionStrings>
<add name="iDjItDBContext"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=DELERIUM-PC;Initial Catalog=iDjItDb;Integrated         Security=True" />
       </connectionStrings>

Or create a constructor in your context and pass the name of the connection string.

public class iDjItDBContext : DBContext
{
     public iDjItDBContext() : base("ConnectionName")
     {
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response. While this removes all errors, I am confused as to where the data is being stored. When I add new data to the application, i can see that it saves (even after restarting the server instance) but there is no data in the specified database on the SQL server, nor is there a database in the App_data folder. Is it just being stored in memory? thanks
Thanks, that seems to have fixed it!
@Eranga I am following this tutorial. Although I have specified the full version of sql server connection string for my data source, I figured the programme has infact utilized the existing application services data source. I want to use my local data source to create the db and do crud via EF. Can you help me out here?

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.