EF Code First不为n:m关系生成连接表

时间:2016-03-09 13:35:08

标签: asp.net-mvc entity-framework ef-code-first

我一直在使用this tutorial作为代码的指南n:m。我通常首先使用db,但对于学校项目,我必须先使用代码...

我的2个课程如下:

public class Product
{
    public Product()
    {
        Stocks = new HashSet<Stock>();
        ServingUnits = new HashSet<Unit>();
        Orders = new HashSet<Order>();
    }
    public int ProductId { get; set; }

    [StringLength(128, MinimumLength = 1)]
    [Required]
    public string Title { get; set; }

    [StringLength(512, MinimumLength = 1)]
    public string Description { get; set; }

    public int ExperationPeriod { get; set; }

    public byte[] Image { get; set; }

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

    //foreign keys
    [Required]
    public int StockUnitId { get; set; }

    [Required]
    public string CreatedById { get; set; }

    [Required]
    public int ProductStateId { get; set; }

    [Required]
    public int ProductTypeId { get; set; }

    //navigation properties

    [ForeignKey("StockUnitId")]
    public virtual Unit StockUnit { get; set; }

    [ForeignKey("CreatedById")]
    public virtual ApplicationUser CreatedBy { get; set; }

    [ForeignKey("ProductStateId")]
    public virtual ProductState ProductState { get; set; }

    [ForeignKey("ProductTypeId")]
    public virtual ProductType ProductType { get; set; }

    public virtual ICollection<Stock> Stocks { get; set; }
    public virtual ICollection<Unit> ServingUnits { get; set; }
    public virtual ICollection<Order> Orders { get; set; }

}

和另一个:

public class Unit
{
    public Unit()
    {
        Orders = new HashSet<Order>();
        ProductsStock = new HashSet<Product>();
        ProductsServe = new HashSet<Product>();
    }
    public int UnitId { get; set; }

    [StringLength(255, MinimumLength = 1)]
    [Required]
    public string Title { get; set; }

    [StringLength(128, MinimumLength = 1)]
    public string ShortName { get; set; }
    public string Description { get; set; }

    [Required]
    public double BasisFactor { get; set; }

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

    //foreign keys
    [Required]
    public string CreatedById { get; set; }

    //navigation properties    
    [ForeignKey("CreatedById")]
    public virtual ApplicationUser CreatedBy { get; set; }

    public virtual ICollection<Product> ProductsStock { get; set; }
    public virtual ICollection<Product> ProductsServe { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

但是连接表并不存在于我的数据库中。谁能看到我做错了什么?

由于

0 个答案:

没有答案