我正在关注两个模型
Ledger.cs
public class Ledger
{
[Key]
public int ID { get; set; }
[Display(Name = "Name")]
[StringLength(50)]
[Required(ErrorMessage = "Ledger name can not be empty")]
public string NAME { get; set; }
[ForeignKey("MasterGroup")]
[Display(Name = "Group")]
[Required(ErrorMessage = "Group can not be empty")]
public int GROUPID { get; set; }
[Display(Name = "Category")]
[StringLength(50)]
[Required(ErrorMessage = "Category can not be empty")]
public string CATEGORY { get; set; }
[Display(Name = "Short Code")]
[Required(ErrorMessage = "Short code can not be empty")]
[StringLength(50)]
public string SHORTCODE { get; set; }
[ScaffoldColumn(false)]
public DateTime CREATE_TIMESTAMP { get; set; }
[ScaffoldColumn(false)]
public DateTime LAST_EDIT_TIMESTAMP { get; set; }
public virtual MasterGroup MasterGroup { get; set; }
public List<Voucher> DrLedger { get; set; }
public List<Voucher> CrLedger { get; set; }
}
Voucher.cs
public class Voucher
{
[Key]
public int ID { get; set; }
[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[Required]
public DateTime DATE { get; set; }
[Required]
[Display(Name = "Party")]
public int DRPARTYID { get; set; }
[Required]
[Display(Name = "Party")]
public int CRPARTYID { get; set; }
[Required]
[Display(Name = "Amount")]
public decimal AMOUNT { get; set; }
[Display(Name = "Narration")]
public string NARRATION { get; set; }
[ScaffoldColumn(false)]
[ForeignKey("MasterUser")]
public string USERPASSWORD { get; set; }
[ScaffoldColumn(false)]
public DateTime CREATE_TIMESTAMP { get; set; }
[ScaffoldColumn(false)]
public DateTime LAST_EDIT_TIMESTAMP { get; set; }
public virtual MasterUser MasterUser { get; set; }
[ForeignKey("DRPARTYID")]
public virtual Ledger DrLedger { get; set; }
[ForeignKey("CRPARTYID")]
public virtual Ledger CrLedger { get; set; }
}
我想在这两个实体之间创建FK关系。
Ledger's
ID
列,因为PK将指向两次Voucher's
DRPARTYID
和CRPARTYID
作为两个外键关系。
我正在关注迁移代码,我不希望将Ledger_ID
生成为FK。
迁移代码
public partial class AddedVoucherModelAndFKRelationsWithOtherModels : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Voucher",
c => new
{
ID = c.Int(nullable: false, identity: true),
DATE = c.DateTime(nullable: false),
DRPARTYID = c.Int(nullable: false),
CRPARTYID = c.Int(nullable: false),
AMOUNT = c.Decimal(nullable: false, precision: 18, scale: 2),
NARRATION = c.String(),
USERPASSWORD = c.String(maxLength: 50),
CREATE_TIMESTAMP = c.DateTime(nullable: false),
LAST_EDIT_TIMESTAMP = c.DateTime(nullable: false),
Ledger_ID = c.Int(),//THIS SHOULD NOT BE GENERATED
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Ledger", t => t.CRPARTYID, cascadeDelete: true)
.ForeignKey("dbo.Ledger", t => t.DRPARTYID, cascadeDelete: true)
.ForeignKey("dbo.MasterUser", t => t.USERPASSWORD)
.ForeignKey("dbo.Ledger", t => t.Ledger_ID)
.Index(t => t.DRPARTYID)
.Index(t => t.CRPARTYID)
.Index(t => t.USERPASSWORD)
.Index(t => t.Ledger_ID);
}
public override void Down()
{
DropForeignKey("dbo.Voucher", "Ledger_ID", "dbo.Ledger");
DropForeignKey("dbo.Voucher", "USERPASSWORD", "dbo.MasterUser");
DropForeignKey("dbo.Voucher", "DRPARTYID", "dbo.Ledger");
DropForeignKey("dbo.Voucher", "CRPARTYID", "dbo.Ledger");
DropIndex("dbo.Voucher", new[] { "Ledger_ID" });
DropIndex("dbo.Voucher", new[] { "USERPASSWORD" });
DropIndex("dbo.Voucher", new[] { "CRPARTYID" });
DropIndex("dbo.Voucher", new[] { "DRPARTYID" });
DropTable("dbo.Voucher");
}
}
如何将单个列从一个实体连接到另一个实体的两列作为指定列上的PK FK关系?