昨天我的问题解决后,我实施了解决方案,但无法让它工作。这次我会告诉你所有必要的模型:
型号:
项:
[Table("NEOV_Entries")]
public class Entry {
[Key]
public int EntryId { get; set; }
[Display(Name = "Request Creation Date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd.MM.yyyy hh:mm}")]
public DateTime DatasetCreationDate { get; set; }
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
[Required]
public DateTime EntryDate { get; set; }
[Required]
public virtual Employee Employee { get; set; }
public virtual Employee Sponsor { get; set; }
public bool IsResolved { get; set; }
public DateTime? ResolutionDate { get; set; }
public string ResolvedBy { get; set; }
[Display(Name = "Hardware Status")]
public virtual List<EntryHardware> RequestedHardware { get; set; }
[Display(Name = "Workplace Status")]
public ReadyState IsWorkplaceReady { get; set; }
[Display(Name = "Checklist Status")]
public bool IsChecklistArrived { get; set; }
[Display(Name = "Remark")]
public string Comment { get; set; }
public string GetBootstrapRowColor()
{
var diff = (EntryDate - DateTime.Now).Days;
if (IsResolved) {
return "success";
} else if (diff < 0 && !IsResolved) {
return "danger";
} else if (diff < 7 && !IsResolved) {
return "warning";
} else return "";
}
}
设备:
[Table("NEOV_Hardware")]
public class Hardware {
[Key]
public int HardwareId { get; set; }
[Required]
[StringLength(50)]
public string Description { get; set; }
public override string ToString() {
return Description;
}
}
EntryHardware:
[Table("NEOV_EntryHardware")]
public class EntryHardware {
[Key, Column(Order = 0)]
public int EntryId { get; set; }
[Key, Column(Order = 1)]
public int HardwareId { get; set; }
public virtual Entry Entry { get; set; }
public virtual Hardware Hardware { get; set; }
public HardwareStatus Status { get; set; }
}
每次我添加迁移时,都会创建一个名为“EntryEntryHardwares”的新附加表。
迁移代码段:
CreateTable(
"dbo.EntryEntryHardwares",
c => new
{
Entry_EntryId = c.Int(nullable: false),
EntryHardware_EntryId = c.Int(nullable: false),
EntryHardware_HardwareId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Entry_EntryId, t.EntryHardware_EntryId, t.EntryHardware_HardwareId })
.ForeignKey("dbo.NEOV_Entries", t => t.Entry_EntryId, cascadeDelete: true)
.ForeignKey("dbo.NEOV_EntryHardware", t => new { t.EntryHardware_EntryId, t.EntryHardware_HardwareId }, cascadeDelete: true)
.Index(t => t.Entry_EntryId)
.Index(t => new { t.EntryHardware_EntryId, t.EntryHardware_HardwareId });
}
虽然我已经创建了join-Model并且EF创建了对应表(称为NEOV_EntryHardware):
CreateTable(
"dbo.NEOV_EntryHardware",
c => new
{
EntryId = c.Int(nullable: false),
HardwareId = c.Int(nullable: false),
Status = c.Int(nullable: false),
Entry_EntryId = c.Int(),
})
.PrimaryKey(t => new { t.EntryId, t.HardwareId })
.ForeignKey("dbo.NEOV_Entries", t => t.Entry_EntryId)
.ForeignKey("dbo.NEOV_Hardware", t => t.HardwareId, cascadeDelete: true)
.Index(t => t.HardwareId)
.Index(t => t.Entry_EntryId);
以下是迁移脚本中的其他表:
CreateTable(
"dbo.NEOV_Hardware",
c => new
{
HardwareId = c.Int(nullable: false, identity: true),
Description = c.String(nullable: false, maxLength: 50),
PerformanceType = c.Int(),
FormType = c.Int(),
Size = c.Short(),
MonitorColor = c.Int(),
Discriminator = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.HardwareId);
CreateTable(
"dbo.NEOV_Entries",
c => new
{
EntryId = c.Int(nullable: false, identity: true),
DatasetCreationDate = c.DateTime(nullable: false),
EntryDate = c.DateTime(nullable: false),
IsResolved = c.Boolean(nullable: false),
ResolutionDate = c.DateTime(),
ResolvedBy = c.String(),
IsWorkplaceReady = c.Int(nullable: false),
IsChecklistArrived = c.Boolean(nullable: false),
Comment = c.String(),
Employee_Id = c.Int(nullable: false),
Sponsor_Id = c.Int(),
})
.PrimaryKey(t => t.EntryId)
.ForeignKey("dbo.NEOV_Employees", t => t.Employee_Id, cascadeDelete: true)
.ForeignKey("dbo.NEOV_Employees", t => t.Sponsor_Id)
.Index(t => t.Employee_Id)
.Index(t => t.Sponsor_Id);
此外:
如果我让它工作,我是否必须为EntryHardware创建一个自己的Repository(以及Context中的属性等)或者是否足以将EntryHardware对象分配给Entry.RequestedHardware List并保存它? / p>
答案 0 :(得分:0)
我的不好,我只是忘了删除几行关系代码(fluent-api)...删除并重新编译后一切正常。