尝试播种数据库时出现更新错误。不确定我的结构是否正确设置,但基本上您创建了一个包含非常基本信息的案例文件,然后您通过创建链接到案例文件的 ot 或 pt 案例文件来创建一个更复杂的文件(我希望这是半可理解的)
我为了更容易查看我将目录链接到一些代码的数据以及我得到的堆栈跟踪错误。这只发生在我在 Configuration.cs 文件的种子方法中添加案例文件代码时。在某个地方,它依赖于 casefile 与 keywordId 和 settingId 之间的外键关系的旧定义,这些定义在该类的早期版本中存在,但已被删除。
我尝试删除数据库并使用包管理器添加迁移初始值,这给了我创建了这些外键的文件“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; }
}
}
任何帮助,将不胜感激。