4

I'm reading the book "Programming Entity Framework: DbContext", by Julia Lerman and Rowan Miller.

I've downloaded the "BAGA" solution from http://www.thedatafarm.com/learnentityframework/downloadfiles/dbcontext/StartingSolution.zip. I feel like a complete tool here because I can't get any of the code samples to run on my computer.

Normally, when I work with Entity Framework, I have a connection string in my configuration that tells Entity Framework what database to connect to and what credentials to use.

However, in this BAGA solution, I cannot find a connection string or a reference to a database anywhere in the solution. I'm guessing that the code is supposed to all run locally or somesuch, but when I type in and run any of the sample queries or updates, the computer pauses for about 20 seconds and then the following exception is thrown form the SaveChanges method: "The provider did not return a ProviderManifestToken string".

I'm guessing that this solution is supposed to either use an internal database, or I'm supposed to type a connection string somewhere, but I don't see where in the book it tells me how to make this solution work.

It does clearly state that the solution uses EF Code First.

The console app code in BAGA looks like this:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using DataAccess;
using Model;

namespace BreakAwayConsole
{
  class Program
  {
    static void Main(string[] args)
    {
      Database.SetInitializer(new InitializeBagaDatabaseWithSeedData());

      // Call the latest example method here

      // NOTE: Some examples will change data in the database. Ensure that you only call the 
      //       latest example method. The InitializeBagaDatabaseWithSeedData database initializer 
      //       (registered above) will take care of resetting the database before each run.

      AddMachuPicchu();

    }

    // Add example methods here

    private static void AddMachuPicchu()
    {
        using (var context = new BreakAwayContext())
        {
            var machuPicchu = new Destination
            {
                Name = "Machu Picchu",
                Country = "Peru"
            };
            context.Destinations.Add(machuPicchu);
            context.SaveChanges();
        }    
    }    
  }
}

As @Wyktor Zychla asked, here is the code to BreakAwayContext.cs:

using System.Data.Entity;
using Model;

namespace DataAccess
{
  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; }
  }
}

As you can see here, there is no constructor specified.

Vivian River
  • 31,198
  • 62
  • 198
  • 313

2 Answers2

1

Found a solution elsewhere and am adding it here for anyone who has the same issue when they purchase the books. To implement this, add a connection string to your App.config file in the BreakAwayConsole project:

<connectionStrings>
    <add name="BreakAwayContext"
        providerName="System.Data.SqlClient"
        connectionString="Server=localhost;Database=Baga;Integrated Security=True;"/>
  </connectionStrings>

and in your BreakAwayConsole Program.cs Main() method:

Database.SetInitializer(new InitializeBagaDatabaseWithSeedData());
var ctx = new BreakAwayContext();
ctx.Database.Initialize(true);

See this thread which provided the above solution: Entity Framework Database.SetInitializer simply not working

Community
  • 1
  • 1
epik phail
  • 195
  • 8
  • You don't need the Initialize, because your code is calling SaveChanges, this will trigger the seed initialization. You only want to use Initialize if you aren't going to write anything to the database right away. – Erik Funkenbusch Sep 04 '13 at 01:31
  • Only need to add to the app.config and the sample solution just works. No need to call the Database.initialize. – Agus Riyadi Apr 23 '14 at 11:14
0

That should be simple. Find the code with the BreakAwayContext type and look for its constructors. Either they provide an explicit connection string or they use a connection string name.

In both cases you can easily create a proper database.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • I've added the code for `BreakAwayContext`. There is no constructor declared, so where would I put the connection string? – Vivian River May 29 '13 at 21:46
  • If there is no constructor, the EF looks for a connection string of the form: name="DataAccess.BreakAwayContext" connectionString="yourconnectionstringgorshere" provider="System.Data.SqlClient" in the connectionstrings section of the default configuration file. – Wiktor Zychla May 29 '13 at 22:29
  • Thanks. I'll try this when I get back tomorrow. – Vivian River May 29 '13 at 22:32