0

I have an ASP.NET MVC project in VS 2012. I want to use Entity Framework 6 code-first. When I create model and db-context file and when I run my project it does not create any database in my SQK Server. I want to know what is wrong?

I want to know how can I create database by code first in my SQL Server not in SQL Server Express or Compact. How can I create it? I try scaffolding too but it dos not work true and does not create any database. Any tips or trick would be welcome

This is my web.config setting :

<connectionStrings>
 <add name="dbRaja" 
 connectionString="Data Source=hp-PC;Initial Catalog=Raja;User ID=sa;Password=123;MultipleActiveResultSets=True;Trusted_Connection=False;Persist Security Info=True"
 providerName="System.Data.SqlClient" />
</connectionStrings> 

and its my datacontext :

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Raja1.Models
{
 public class dbRaja : DbContext
 {
 // You can add custom code to this file. Changes will not be overwritten.
 // 
 // If you want Entity Framework to drop and regenerate your database
 // automatically whenever you change your model schema, add the following
 // code to the Application_Start method in your Global.asax file.
 // Note: this will destroy and re-create your database with every model change.
 // 
 // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<Raja1.Models.Raja1Context>());
 public DbSet<Raja1.Models.Spareparts> Spareparts { get; set; }
 }
}
marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Jul 7, 2013 at 7:11
12
  • What happens and what does not? Code looks basically OK. Commented Jul 7, 2013 at 7:19
  • i dont know why it dosent create my database when it run ??? Commented Jul 7, 2013 at 7:23
  • 3
    No, it creates your Db when you do the first query. Commented Jul 7, 2013 at 7:31
  • 1
    Run a query. You should either get a Db or an exception. Commented Jul 7, 2013 at 7:37
  • 2
    Alway include exception and error messages in the question. Commented Jul 7, 2013 at 7:45

2 Answers 2

2

You need to tell your DbContext to use the intended connection string. It will default to one named the same as your application (i.e. "Raja1"), whereas your connection string is named "dbRaja".

public class MyContext : DbContext
{
 public MyContext : base("name=dbRaja")
 {
 }
}
answered Jul 8, 2013 at 16:45
1

Put the following code in your app start:

using (var context = new YOUR_DB_CONTEXT()) {
 if (!context.Database.Exists()) {
 ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
 }
}
answered Jul 8, 2013 at 17:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.