【发布时间】:2016-08-01 07:37:18
【问题描述】:
由于某种原因,这个“小型”示例似乎无法在我的电脑上运行,我是不是做错了什么?
public class ResourceSection
{
[Key]
public int Id { get; set; }
[ForeignKey("ParentSection")]
public int? ParentSectionId { get; set; }
[Required]
public string Name { get; set; }
public string Path { get { return ParentSectionId != null ? ParentSection.Path + "." + Name : Name; } }
public virtual ResourceSection ParentSection { get; set; }
public virtual ICollection<ResourceSection> ChildSections { get; set; }
public virtual ICollection<ResourceString> Resources { get; set; }
}
public class ResourceString
{
[Key]
public int Id { get; set; }
[ForeignKey("Section")]
public int SectionId { get; set; }
[Required(AllowEmptyStrings = true)]
public string Culture { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string DisplayName { get; set; }
[Required]
public string ShortDisplayName { get; set; }
[Required]
public string Description { get; set; }
public virtual ResourceSection Section { get; set; }
}
public class ResourcesContext : DbContext
{
public DbSet<ResourceSection> Sections { get; set; }
public DbSet<ResourceString> Resources { get; set; }
}
public class ResourcingTests
{
public void TestResourceContextInit()
{
// init the db
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ResourcesContext, Configuration>());
// build the context
var foo = new ResourcesContext();
// do some sort of query on it
var sections = foo.Sections.Where(s => s.Id > 0).ToList();
// jobs a good un right? ... right?
Console.WriteLine("Resources initialised");
}
}
...我在控制台上显示“资源已初始化”,但我的理解是这也应该创建我的数据库。 连接字符串绝对正确,我只是为迁移内容添加了一个空配置类,其中添加了“AutomaticMigrationsEnabled = false;”在 ctor 中,我还使用包管理器控制台中的“add-migration Initial”编写了初始迁移脚本,该控制台正确地创建了工作迁移。
我可以通过调用“update-database”来测试这一切是否有效,并查看它是否生成了正确的数据库。
那么为什么它不像我所有其他上下文那样“自动”创建我的数据库呢?
旁注:
这是从一个更大的 Web 项目中删除到控制台应用程序中的代码的摘录,通常我使用 msdeploy 通过简单地部署代码来推出新的数据库,但这似乎会导致 IIS 在处理第一个时简单地冻结请求。
编辑:丢失的位...
这是生成的迁移:
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.ResourceStrings",
c => new
{
Id = c.Int(nullable: false, identity: true),
SectionId = c.Int(nullable: false),
Culture = c.String(nullable: false),
Name = c.String(nullable: false),
DisplayName = c.String(nullable: false),
ShortDisplayName = c.String(nullable: false),
Description = c.String(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.ResourceSections", t => t.SectionId, cascadeDelete: true)
.Index(t => t.SectionId);
CreateTable(
"dbo.ResourceSections",
c => new
{
Id = c.Int(nullable: false, identity: true),
ParentSectionId = c.Int(),
Name = c.String(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.ResourceSections", t => t.ParentSectionId)
.Index(t => t.ParentSectionId);
}
public override void Down()
{
DropForeignKey("dbo.ResourceStrings", "SectionId", "dbo.ResourceSections");
DropForeignKey("dbo.ResourceSections", "ParentSectionId", "dbo.ResourceSections");
DropIndex("dbo.ResourceSections", new[] { "ParentSectionId" });
DropIndex("dbo.ResourceStrings", new[] { "SectionId" });
DropTable("dbo.ResourceSections");
DropTable("dbo.ResourceStrings");
}
}
...这是我的配置类(在应用程序上启用迁移时也由 EF/Microsoft 生成)...
internal sealed class Configuration : DbMigrationsConfiguration<Tests.ResourcesContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Tests.ResourcesContext context)
{
}
}
...正如我上面所说,如果我使用包管理器控制台中的“update-database”命令一切正常,我似乎无法运行代码并让它自己生成数据库。
【问题讨论】:
标签: c# entity-framework recursion dbcontext