“编程实体框架代码第一本书”中的示例不创建数据库

时间:2014-01-11 13:24:35

标签: c# entity-framework ef-code-first

I was download examples from o'reilly site但他们不会创建.sdf文件。在本书中描述,默认情况下,数据库是在bin / debug项目文件夹中创建的。我已经安装了所有需要的组件。 Debuging成功通过。这里是源代码:

namespace BreakAwayConsole
{
  class Program
  {
    static void Main(string[] args)
    {
      Database.SetInitializer(
        new DropCreateDatabaseIfModelChanges<BreakAwayContext>());

      InsertDestination();
    }

    private static void InsertDestination()
    {
      var destination = new Destination
      {
        Country = "Indonesia",
        Description = "EcoTourism at its best in exquisite Bali",
        Name = "Bali"
      };

      using (var context = new BreakAwayContext())
      {
        context.Destinations.Add(destination);
        context.SaveChanges();
      }
    }
  }
}

namespace DataAccess
{
  public class BreakAwayContext : DbContext
  {
    public DbSet<Destination> Destinations { get; set; }
    public DbSet<Lodging> Lodgings { get; set; }
  }
}
namespace Model
{
  public class Lodging
  {
    public int LodgingId { get; set; }
    [Required]
    [MaxLength(200)]
    [MinLength(10)]
    public string Name { get; set; }
    public string Owner { get; set; }
    public bool IsResort { get; set; }

    public Destination Destination { get; set; }
  }
  public class Destination
  {
    public int DestinationId { get; set; }
    [Required]
    public string Name { get; set; }
    public string Country { get; set; }
    [MaxLength(500)]
    public string Description { get; set; }
    [Column(TypeName = "image")]
    public byte[] Photo { get; set; }

    public List<Lodging> Lodgings { get; set; }
  }
}

1 个答案:

答案 0 :(得分:1)

如果尚未为上下文指定连接字符串,则默认情况下将在SQLEXPRESS服务器上创建数据库,而不是在调试文件夹中创建sdf文件。在本地SQLEXPRESS实例上搜索名为DataAccess.BreakAwayContext的数据库。