实体框架CodeFirst在非主键上创建2个表之间的关系

时间:2016-06-23 13:42:49

标签: c# entity-framework-6

您好我正在尝试添加迁移并将表的主键更改为主键,因为这已成为一项要求。现在说它将永远是独一无二的,但需要有一对多的关系。与其他表和更新级联。

我找到了this但是我没有使用DataAnnotations,并且想要这样做。

以下是有关的2个课程。

public partial class MProcurement
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public MProcurement()
    {
        Logs = new List<MLog>();
    }
    [Key]
    public int Id { get; set; }
    [Required]
    [StringLength(8)]
    [Index(IsUnique = true)]
    public string ParcelId { get; set; } //current primary key that i want to change
    [Required]
    public DateTime PurchaseDate { get; set; }
    [Required]
    public string UserId { get; set; }
    [Required]
    [StringLength(10)]
    public string ProducerCode { get; set; }
    public FscCertType FscCertType { get; set; }
    public int CountyId { get; set; }
    [Required]
    [StringLength(5)]
    public string Zip { get; set; }
    public Scale  ProducerScale { get; set; }
    public Scale Scale { get; set; }
    public Terms Terms { get; set; }
    public bool UseProducerScale { get; set; }


    [ForeignKey(nameof(UserId))]
    public virtual MUser User { get; set; }
    [ForeignKey(nameof(ProducerCode))]
    public virtual MProducer Producer { get; set; }
    [ForeignKey(nameof(CountyId))]
    public virtual MCounty County { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual List<MLog> Logs { get; set; }
}


public partial class MLog
{
    public int Id { get; set; }
    public int? Tag { get; set; }

    [Required]
    [MinLength(8), MaxLength(8)]
    [Index("IX_Proc_Seq", 1, IsUnique = true)]
    [ForeignKey(nameof(MProcurement.ParcelId))]//would like to add to MProcurement as ForignKey
    public string ProcurementId { get; set; }

    [Required]
    public DateTime PurchaseDate { get; set; }

    public Scale Scale { get; set; }
    [Required]
    [Index("IX_Proc_Seq",2,IsUnique = true)]
    public int Seq { get; set; }

    public bool DoubleLength { get; set; }
    [Required]
    [MaxLength(4)]
    public string SpecieId { get; set; }
    [Required]
    public int CategoryId { get; set; }
    [Required]
    [MaxLength(1)]
    public string Grade { get; set; }
    [Required]
    public int Length { get; set; }
    [Required]
    public int CutbackLength { get; set; }
    [Required]
    public int CutbackFeet { get; set; }
    [Required]
    public int Diameter { get; set; }
    public int? Pounds { get; set; }
    [Required]
    public int Feet { get; set; }
    [Required]
    public decimal NetCost { get; set; }
    [Required]
    public decimal AdjCostPerFoot { get; set; }
    [Required]
    public decimal AdjustedNet { get; set; }
    [Required]
    public decimal ProducerPricePerFoot { get; set; }
    [Required]
    public decimal CostPerFoot { get; set; }
    [Required]
    public int ProducerFeet { get; set; }

    public virtual MCategory Category { get; set; }
    public virtual MSpecie Specie { get; set; }


    public virtual MProcurement Procurement { get; set; }

}

0 个答案:

没有答案