データベースをシードしようとすると、更新エラーが発生します。私の構造が正しくセットアップされているかどうかはわかりませんが、基本的には非常に基本的な情報を含むケースファイルを作成し、ケースファイルにリンクされた ot または pt ケースファイルを作成してより複雑なファイルを作成します (これが半分理解できることを願っています)。
ディレクトリをコードの一部にリンクしているデータと、取得しているスタックトレースエラーを表示しやすくするために、これを少し簡単にするようにします。これは、Configuration.cs ファイルのシード メソッドにケースファイル コードを追加した場合にのみ発生します。ケースファイルと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; }
}
}
どんな助けでも大歓迎です。