简体   繁体   中英

Entity Framework - Code first with m:m and additional properties

I'm refering to the Original question in addition to this Solution

After my problem got solved yesterday I implemented the solution but can't get it to work. I will show you all my neccessary models this time:

Models:

Entry:

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

Hardware:

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

Nov everytime I add a migration a new additional table called "EntryEntryHardwares" gets created.

Migration snippet:

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

    }

although I already created the join-Model and EF creates the correspondig table (called 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);

Here are the other tables in the migration script:

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

Additionally:

If I get this to work, do I have to create a own Repository (and own Property inside the Context etc.) for EntryHardware or would it be enough to assign EntryHardware objects to the Entry.RequestedHardware List and save it?

不好,我只是忘了删除几行关系代码(fluent-api)...删除并重新编译后,一切正常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM