MVC4 EF数据库创建和种子问题

时间:2013-08-19 16:53:05

标签: entity-framework asp.net-mvc-4 localdb

尝试播种数据库时出现更新错误。不确定我的结构是否正确设置但基本上你创建了一个包含非常基本信息的casefile,然后通过创建一个链接到casefile的ot或pt casefile创建一个更复杂的文件(我希望这是半易懂的)< / p>

为了让我更容易查看我正在linking the directory的某些代码和我得到的堆栈跟踪错误的数据。这只发生在我在Configuration.cs文件的种子方法中添加casefile代码时。在某个地方,它挂起了一个旧的定义,用于casefile和keywordId以及settingId之间的外键关系,它们在类的早期版本中被删除了。

我尝试删除数据库,并使用Package manager添加迁移初始化,这给了我创建了这些foriegn密钥的文件'201308191626305_initial.cs'。我尝试在运行'update-database -verbose'之前手动从文件中删除这些文件,但这仍然会产生错误,抱怨堆栈轨道中的Keyword-KeywordId。所有表都正确构建并填充,除了正确创建但未填充的casefile表。

在此处使用源文件进行更新。非常大的文件...

    namespace MCPD_v3.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;

        public partial class initial : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.Casefiles",
            c => new
                {
                    CasefileId = c.Int(nullable: false, identity: true),
                    UserId = c.Int(nullable: false),
                    ClientId = c.Int(nullable: false),
                    PTCasefileId = c.Int(),
                    OTCasefileId = c.Int(),
                    Submitted = c.Boolean(nullable: false),
                    Postable = c.Boolean(nullable: false),
                    Setting_SettingId = c.Int(),
                    Keyword_KeywordId = c.Int(),
                })
            .PrimaryKey(t => t.CasefileId)
            .ForeignKey("dbo.Clients", t => t.ClientId, cascadeDelete: true)
            .ForeignKey("dbo.Settings", t => t.Setting_SettingId)
            .ForeignKey("dbo.UserProfile", t => t.UserId, cascadeDelete: true)
            .ForeignKey("dbo.OTCasefiles", t => t.OTCasefileId)
            .ForeignKey("dbo.PTCasefiles", t => t.PTCasefileId)
            .ForeignKey("dbo.Keywords", t => t.Keyword_KeywordId)
            .Index(t => t.ClientId)
            .Index(t => t.Setting_SettingId)
            .Index(t => t.UserId)
            .Index(t => t.OTCasefileId)
            .Index(t => t.PTCasefileId)
            .Index(t => t.Keyword_KeywordId);
    ...

    CreateTable(
            "dbo.Keywords",
            c => new
                {
                    KeywordId = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Expanded = c.String(),
                })
            .PrimaryKey(t => t.KeywordId);

    }

    public override void Down()
    {
        DropIndex("dbo.Clients", new[] { "SettingId" });
        DropIndex("dbo.Clients", new[] { "GenderId" });
        DropIndex("dbo.Casefiles", new[] { "Keyword_KeywordId" });
        DropIndex("dbo.Casefiles", new[] { "PTCasefileId" });
        DropIndex("dbo.Casefiles", new[] { "OTCasefileId" });
        DropIndex("dbo.Casefiles", new[] { "UserId" });
        DropIndex("dbo.Casefiles", new[] { "Setting_SettingId" });
        DropIndex("dbo.Casefiles", new[] { "ClientId" });
        DropForeignKey("dbo.Clients", "SettingId", "dbo.Settings");
        DropForeignKey("dbo.Clients", "GenderId", "dbo.Genders");
        DropForeignKey("dbo.Casefiles", "Keyword_KeywordId", "dbo.Keywords");
        DropForeignKey("dbo.Casefiles", "PTCasefileId", "dbo.PTCasefiles");
        DropForeignKey("dbo.Casefiles", "OTCasefileId", "dbo.OTCasefiles");
        DropForeignKey("dbo.Casefiles", "UserId", "dbo.UserProfile");
        DropForeignKey("dbo.Casefiles", "Setting_SettingId", "dbo.Settings");
        DropForeignKey("dbo.Casefiles", "ClientId", "dbo.Clients");
        DropTable("dbo.Keywords");
        DropTable("dbo.Files");
        DropTable("dbo.PTCasefiles");
        DropTable("dbo.OTCasefiles");
        DropTable("dbo.UserProfile");
        DropTable("dbo.Settings");
        DropTable("dbo.Genders");
        DropTable("dbo.Clients");
        DropTable("dbo.Casefiles");
    }

这是类文件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Text;

    namespace MCPD_v3.Models
    {
        public class Casefile
        {

            public int CasefileId { get; set; }
            public int UserId { get; set; }
            public int ClientId { get; set; }
            public int? PTCasefileId { get; set; }
            public int? OTCasefileId { get; set; }

            public bool Submitted { get; set; }
            public bool Postable { get; set; }


            public virtual Client Client { get; set; }

            public virtual UserProfile User { get; set; }

            public virtual OTCasefile OTCasefile { get; set; }

            public virtual PTCasefile PTCasefile { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace MCPD_v3.Models
    {
        public class Keyword
        {
            public Keyword()
            {
                this.Casefiles = new HashSet<Casefile>();
            }

            public int KeywordId { get; set; }
            public string Name { get; set; }
            public string Expanded { get; set; }

            public ICollection<Casefile> Casefiles { get; set; }
        }
    }

任何帮助都将不胜感激。

0 个答案:

没有答案