实体框架-首先使用m:m和其他属性编写代码

本文关键字:属性 其他 代码 -首 实体 框架 | 更新日期: 2023-09-27 18:12:58

我指的是除了这个解决方案之外的原始问题

昨天我的问题得到解决后,我实施了解决方案,但不能让它工作。这次我将给你看所有我需要的模型:

模型:

条目:

[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; }
}

11每次我添加一个迁移,一个新的额外的表名为"entryentryhardware"被创建。

迁移代码片段:

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 });
    }

虽然我已经创建了连接模型,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创建一个自己的存储库(并在上下文中拥有自己的属性等),或者将EntryHardware对象分配给Entry就足够了。请求的硬件列表并保存它?

实体框架-首先使用m:m和其他属性编写代码

我的错,我只是忘记删除关系代码的几行(流利的api)…在删除和重新编译之后,一切都正常了。