【问题标题】:Recursion in EF seems to not initialise the context at allEF 中的递归似乎根本没有初始化上下文
【发布时间】: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


    【解决方案1】:

    从你的问题中运行代码对我来说是正确的,所以它会暗示你没有向我们展示的位有问题。此外,如果不调用创建的上下文,代码就无法进入“资源初始化”,因此这表明您的连接字符串可能存在问题。

    我将首先使用断点调用它,检查foo 对象,特别是检查创建的数据库的连接字符串。也许它是在您没有注意到的地方创建的,比如在 localdb 连接上?如果是这样,那么它可能使用了自动连接字符串,而不是您期望的。

    【讨论】:

    • 是的,这是我在问之前的怀疑。我的网络堆栈有 10 个其他数据库,所有连接字符串都是相同的,除了数据库名称,所以我看不出这会如何破坏它......好吧,至少它只是我机器本地的东西......等待我用调试器打了它
    • 哦,这很奇怪...如果我实际上在数据库中添加一个部分并在执行选择查询之前调用 savechanges 它会创建数据库...多么奇特!
    • 哦,谢谢您的帮助...看来代码根本没有损坏:s
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2015-08-23
    • 2013-06-04
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多