实体框架 - 从一对多切换到多对多

时间:2015-06-23 14:00:16

标签: asp.net-mvc entity-framework

我有3个模型,您可以在下面看到名为ProductModel,ProductsModel和CategoryModel。目前,许多产品可以只在一个类别中。但我需要切换它,以便产品可以在多个类别中。我该怎么做?

CategoryModel:

public class CategoryModel
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Required")]
    [Display(Name = "Category Name")]
    [MaxLength(50)]
    public String categoryName { get; set; }

    [MaxLength(50)]
    public String categoryDBName { get; set; }

    [DefaultValue(true)]
    [Display(Name = "Active?")]
    public bool isActive { get; set; }

}

产品型号:

public class ProductModel
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Required")]
    [Index("ItemNumber", 1, IsUnique = true)]
    [Display(Name = "Item #")]
    public int itemNumber { get; set; }

    [Required(ErrorMessage = "Required")]
    [Display(Name = "Product")]
    [MaxLength(50)]
    public String product { get; set; }

    [Display(Name = "Description")]
    [MaxLength(500)]
    public String description { get; set; }

    [DefaultValue(true)]
    [Display(Name = "Active?")]
    public bool active { get; set; }

    [Display(Name = "Image Name")]
    public String imageName { get; set; }

    [Display(Name = "PDF Name")]
    public String PDFName { get; set; }

    [ForeignKey("Category")]
    public int CategoryID { get; set; }

    public virtual CategoryModel Category { get; set; }

    public IEnumerable<SelectListItem> CategoryList { get; set; }

    public virtual BrochureModel Brochure { get; set; }

    public IEnumerable<SelectListItem> BrochureList { get; set; }

    public static IEnumerable<SelectListItem> getCategories(int id = 0)
    {
        using (var db = new ProductContext())
        {
            List<SelectListItem> list = new List<SelectListItem>();
            var categories = db.Categories.ToList();
            foreach (var cat in categories)
            {
                SelectListItem sli = new SelectListItem { Value = cat.ID.ToString(), Text = cat.categoryName };

                if (id > 0 && cat.ID == id)
                {
                    sli.Selected = true;
                }
                list.Add(sli);
            }
            return list;
        }

    }

    public ProductModel()
    {
        active = true;
    }

}

ProductsModel:

public class ProductsModel
{
    public List<ProductModel> Products { get; set; }
    public bool hasFrozen { get; set; }

    public static List<ProductModel> getProductsByCat(string category)
    {

        return ProductsModelDB.getProductsByCat(category);

    }
}

1 个答案:

答案 0 :(得分:1)

有几种方法可以实现目标。看起来像你使用注释(vs流畅的api),所以你可以在每一侧添加集合或创建一个桥类(ProductCategory)。

http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

public class CategoryModel
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Required")]
    [Display(Name = "Category Name")]
    [MaxLength(50)]
    public String categoryName { get; set; }

    ...

    public IEnumerable<ProductModel> ProductList { get; set; }

}

public class ProductModel
{
    public int ID { get; set; }

    ...

    public IEnumerable<CategoryModel> CategoryList { get; set; }

    ...

}

就个人而言,我更喜欢这个漂亮的流畅api。https://msdn.microsoft.com/en-us/data/jj591620.aspx#ManyToMany

modelBuilder.Entity<ProductModel>() 
    .HasMany(t => t.CategoryList) 
    .WithMany(t => t.ProductList) 
    .Map(m => 
    { 
        m.ToTable("ProductCategory"); 
        m.MapLeftKey("ProductID"); 
        m.MapRightKey("CategoryID"); 
    });