【问题标题】:Entity Framework - Code first with m:m and additional properties实体框架 - 首先使用 m:m 和其他属性编写代码
【发布时间】:2016-10-14 07:52:59
【问题描述】:

除了这个Solution之外,我指的是Original question

昨天我的问题得到解决后,我实施了解决方案,但无法让它发挥作用。这次我将向您展示我所有必需的模型:

型号:

条目:

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

入口硬件:

[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 月,每次我添加迁移时,都会创建一个名为“EntryEntryHardwares”的新附加表。

迁移sn-p:

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 创建一个自己的存储库(以及在上下文中的自己的属性等),或者将 EntryHardware 对象分配给 Entry.RequestedHardware 列表并保存就足够了吗?

【问题讨论】:

    标签: c# asp.net-mvc entity-framework


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多