【问题标题】:How to get "Programming Entity Framework: DbContext" examples to run?如何运行“编程实体框架:DbContext”示例?
【发布时间】:2013-05-29 21:35:32
【问题描述】:

我正在阅读 Julia Lerman 和 Rowan Miller 所著的“Programming Entity Framework: DbContext”一书。

我已经从http://www.thedatafarm.com/learnentityframework/downloadfiles/dbcontext/StartingSolution.zip 下载了“BAGA”解决方案。我觉得这里是一个完整的工具,因为我无法让任何代码示例在我的计算机上运行。

通常,当我使用 Entity Framework 时,我的配置中有一个连接字符串,它告诉 Entity Framework 要连接到什么数据库以及要使用什么凭据。

但是,在这个 BAGA 解决方案中,我在解决方案的任何地方都找不到连接字符串或对数据库的引用。我猜代码应该全部在本地或类似的地方运行,但是当我输入并运行任何示例查询或更新时,计算机会暂停大约 20 秒,然后从SaveChanges 引发以下异常方法:“提供者没有返回 ProviderManifestToken 字符串”。

我猜这个解决方案应该使用内部数据库,或者我应该在某处键入连接字符串,但我没有看到它在书中的哪个位置告诉我如何使这个解决方案工作.

它明确指出该解决方案使用 EF Code First。

BAGA 中的控制台应用代码如下所示:

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();
        }    
    }    
  }
}

正如@Wyktor Zychla 所问,这是 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; }
  }
}

正如您在此处看到的,没有指定构造函数。

【问题讨论】:

    标签: .net sql entity-framework


    【解决方案1】:

    在其他地方找到了一个解决方案,并在此处为购买书籍时遇到相同问题的任何人添加它。要实现这一点,请在 BreakAwayConsole 项目中的 App.config 文件中添加一个连接字符串:

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

    在您的 BreakAwayConsole Program.cs Main() 方法中:

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

    请参阅提供上述解决方案的此线程:Entity Framework Database.SetInitializer simply not working

    【讨论】:

    • 您不需要Initialize,因为您的代码正在调用SaveChanges,这将触发种子初始化。如果您不打算立即向数据库写入任何内容,则只想使用 Initialize。
    • 只需将 添加到 app.config 中,示例解决方案即可。无需调用 Database.initialize。
    【解决方案2】:

    这应该很简单。查找具有 BreakAwayContext 类型的代码并查找其构造函数。它们要么提供显式连接字符串,要么使用连接字符串名称。

    在这两种情况下,您都可以轻松创建合适的数据库。

    【讨论】:

    • 我添加了BreakAwayContext 的代码。没有声明构造函数,那么我将连接字符串放在哪里?
    • 如果没有构造函数,EF 会在 connectionstrings 部分中查找以下形式的连接字符串:name="DataAccess.BreakAwayContext" connectionString="yourconnectionstringgorshere" provider="System.Data.SqlClient"默认配置文件。
    • 谢谢。我明天回来试试这个。
    猜你喜欢
    • 2016-09-19
    • 2018-05-30
    • 2013-03-07
    • 2017-05-14
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    相关资源
    最近更新 更多