0

I am using code first EF and new to this framework. I am trying to create a database using Database.SetInitializer but it looks like I need SQL Server Express. But I have to create database in SQL Server 2014. How to do this?

Can anybody explain this with the example from EF-dbcontext book which has following classes.

 public class BreakAwayContext : DbContext { public DbSet<Destination> Destinations { get; set; } public DbSet<Lodging> Lodgings { get; set; } public DbSet<Trip> Trips { get; set; } public DbSet<Person> People { get; set; } public DbSet<Reservation> Reservations { get; set; } public DbSet<Payment> Payments { get; set; } public DbSet<Activity> Activities { get; set; } } class Program { static void Main(string[] args) { Database.SetInitializer(new InitializeBagaDatabaseWithSeedData()); try { using (var context = new BreakAwayContext()) { foreach (var destination in context.Destinations) Console.WriteLine(destination.Name); } } catch(Exception ex){ Console.WriteLine(ex.ToString()); } Console.Read(); } } public class InitializeBagaDatabaseWithSeedData : DropCreateDatabaseAlways<BreakAwayContext> { protected override void Seed(BreakAwayContext context) { context.Destinations.Add(new Destination { Name = "Hawaii", Country = "USA", Description = "Sunshine, beaches and fun." }); context.Destinations.Add(new Destination { Name = "Wine Glass Bay", Country = "Australia", Description = "Picturesque sandy beaches." }); } 
2
  • No problem, just change your connection string in the context constructor. If you are using an initializer like DropCreateDatabase... you will need to insure you have the rights to do so. Otherwise you can create the initial database in management studio, use an initializer like MigrateDatabaseToLatestVersion and point your connection string at the new database. Commented Aug 28, 2015 at 19:02
  • i am using a class inherited from DropCreateDatabaseAlways to initialize. I have no idea how to use connection string in this context. Currently I have no connection string. I think default is used when I have no connection string.But I cannot use default in my case. Thanks. Commented Aug 28, 2015 at 19:17

1 Answer 1

1

Set your connection string in your constructor:

public class BreakAwayContext : DbContext { public BreakAwayContext() : base("MyConnectionString", throwIfV1Schema: false) { } ... 

Then set your connection string in web.config or app.config:

<connectionStrings> <add name="MyConnectionString" connectionString="Data Source=servername;Initial Catalog=dbname;..." providerName="System.Data.SqlClient" /> </connectionStrings> 
Sign up to request clarification or add additional context in comments.

3 Comments

I have added the connection string. But the classes ApplicationDbContext and IdentityDbContext<ApplicationUser> are not clear to me. I have updated the question.Can you explain me in this context.Thanks.
Don't worry about IdentityDbContext unless you are using Asp.Net identity. ApplicationDbContext is equivalent to your BreakawayContext. I will edit the answer for your names. So basically, just add the constructor so it points to a connection string in your config file.
I commented two parameters of the constructor and it worked. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.