C# 例如「;编程实体框架代码第一册;don';不要创建数据库
但它们不会创建.sdf文件。在本书中,默认情况下,数据库是在bin/debug项目文件夹中创建的。我已经安装了所有需要的组件。调试成功通过。这里是源代码:C# 例如「;编程实体框架代码第一册;don';不要创建数据库,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,但它们不会创建.sdf文件。在本书中,默认情况下,数据库是在bin/debug项目文件夹中创建的。我已经安装了所有需要的组件。调试成功通过。这里是源代码: namespace BreakAwayConsole { class Program { static void Main(string[] args) { Database.SetInitializer( new DropCreateDatabaseIfModelChanges<Br
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; }
}
}
namespace BreakAwayConsole
{
班级计划
{
静态void Main(字符串[]参数)
{
Database.SetInitializer(
新建DropCreateDatabaseIfModelChanges());
InsertDestination();
}
私有静态void InsertDestination()
{
var destination=新目的地
{
Country=“印度尼西亚”,
Description=“巴厘岛最佳生态旅游”,
Name=“巴厘岛”
};
使用(var context=new BreakAwayContext())
{
context.Destinations.Add(destination);
SaveChanges();
}
}
}
}
命名空间数据访问
{
公共类BreakAwayContext:DbContext
{
公共数据库集目的地{get;set;}
公共数据库集住宿{get;set;}
}
}
名称空间模型
{
公务舱住宿
{
public int lodingId{get;set;}
[必需]
[MaxLength(200)]
[秘书长(10)]
公共字符串名称{get;set;}
公共字符串所有者{get;set;}
公共bool IsResort{get;set;}
公共目的地{get;set;}
}
公务舱目的地
{
public int DestinationId{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串国家{get;set;}
[最大长度(500)]
公共字符串说明{get;set;}
[列(TypeName=“image”)]
公共字节[]Photo{get;set;}
公共列表住宿{get;set;}
}
}
如果尚未为上下文指定连接字符串,则默认情况下,将在SQLEXPRESS server上创建数据库,而不是在调试文件夹中创建sdf文件。在本地SQLEXPRESS实例上搜索名为DataAccess.BreakAwayContext的数据库。本地SQLEXPRESS实例位于何处?我通过windows资源管理器在根文件夹中搜索了它,但没有find@Ark这是一个数据库服务器。打开视图>服务器资源管理器,然后输入服务器名称\SQLEXPRESS
,并在下拉列表中选择您的数据库名称。“找不到”我还尝试连接到服务器“localhost\SQLEXPRESS”,并再次收到“找不到”消息。一个连接总是存在的,它连接到我的名字PC服务器,但在他的下拉列表中我看不到任何连接database@Ark确保在配置文件中没有为上下文配置任何连接字符串。另外,通过查看context.database.Connection.ConnectionString
property@Ark您可以使用SQLServerManagementStudio连接到SQLEXPRESS服务器并管理您的数据—与VisualStudio中内置的服务器资源管理器相比,我有更多的功能