为什么实体框架代码首先一对多不能正常工作

时间:2016-09-04 09:17:44

标签: entity-framework linq code-first

我有两个表类别和产品。我已经在类别表中输入了一些数据,后来我想根据所选类别添加一些产品。当我尝试添加产品时,它不会保存产品数据。这是我为更新或将行附加到Product table

而编写的函数
public  void write(string Name, string Code, decimal Price,
                   string Image, Category Category,string Description)
    {
       using (var x = new MyDB())
      {
            Product pro = new Product
            {
                category = Category,
                CategoryID=Category.ID,
                PName = Name,
                Code = Code,
                Image = Image,
                Description = Description,
                Price = Price
            };     
            var row = x.categorys.Find(Category.ID);
            Category.product.Add(pro); 
            x.Entry(row).CurrentValues.SetValues(Category);
            x.SaveChanges();





        }
    }

这里也是我的表格:

 public class Product
{
 public Product()
    {
        this.order = new HashSet<Order>();
    }
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int32 ID { get; set; }
    public string PName { get; set; }
    public string Code { get; set; }
    public decimal Price { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }
    public Int32 CategoryID { get; set; }
    [ForeignKey("CategoryID")]
    public Category category { get; set; }
    public virtual ICollection<Order> order { get; set; }


}
public class Category
{
    public Category()
    {
        this.product = new HashSet<Product>();
    }
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int32 ID { get; set; }
    [Index(IsUnique =true)]
    [Required, MaxLength(50), MinLength(1)]
    public string Name { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }
    public virtual ICollection<Product> product { get; set; }
}

这是我的DbContext

public class MyDB : DbContext
{

    public MyDB()
        : base("name=MyDB")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDB, coffeeshopapp.Migrations.Configuration>("MyDB"));
    }


    public virtual DbSet<Category> categorys { get; set; }

    public virtual DbSet<Product> products { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

    }
}

有人可以帮我解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:0)

首先,您必须修改模型,如下所示。

public class Product
{

  public Product()
    {
        this.order = new HashSet<Order>();
    }

    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int32 ID { get; set; }

    public string PName { get; set; }
    public string Code { get; set; }
    public decimal Price { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }

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

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


}

public class Category
{
    public Category()
    {
        this.product = new HashSet<Product>();
    }

    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int32 ID { get; set; }

    [Index(IsUnique =true)]
    [Required, MaxLength(50), MinLength(1)]
    public string Name { get; set; }

    public string Description { get; set; }
    public string Image { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

之后Save,如下所示。

public  void write(string Name, string Code, decimal Price,
                   string Image, Category Category,string Description)
    {
       using (var x = new MyDB())
      {
            Product pro = new Product
            {
                Category = Category,
                PName = Name,
                Code = Code,
                Image = Image,
                Description = Description,
                Price = Price
            };     

           x.Products.Add(pro);
           x.SaveChanges();

        }
    }