简体   繁体   English

实体添加新ID而不是使用现有ID

[英]Entity adds new IDs instead using existing one

How can I attach entire list or loop through it without null reference error? 如何附加整个列表或遍历整个列表而没有空引用错误?

Entity classes 实体类

public class Product
    {
        [Key]
        public int Product_Id { get; set; }
        public string Product_Name { get; set; }
        public IList<Category> Categories { get; set; }       
    }

public class Category
    {
        [Key]
        public int Category_ID { get; set; }
        public string Category_Name { get; set; }
        public IList<Product> Products { get; set; }
    }

List from CheckBoxList CheckBoxList中的列表

 IList<ListItem> prodCat = new List<ListItem>();
    foreach (ListItem item in cblCategory.Items)
    {
        if (item.Selected)
        {
            prodCat.Add(item);
        }
    }

Adding new product 新增产品

I've tried to attach Categories - see comment 我尝试附加类别-查看评论

        public void AddProduct(string prodName, IList<ListItem> prodCat)
    {
        ProductDBContext productDBContext = new ProductDBContext();
        Product prodNew = new Product();
        prodNew.Product_Name = prodName;

        List<Category> categList = new List<Category>();
        foreach (ListItem item in prodCat)
        {
            Category categObj = new Category
            {
                Category_Name = item.Text,
                Category_ID = Convert.ToInt32(item.Value)
            };
            categList.Add(categObj);
          //  prodNew.Categories.Add(categObj);  //Null reference
        }
        prodNew.Categories = categList;
        productDBContext.Products.Add(prodNew);
        productDBContext.SaveChanges();
    }
var categories = prodCat
    .Select(p => new Category
    {
        Category_ID = p.Value,
        Category_Name = p.Text
    }
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC5-多对多使用ApplicationUser添加新行,而不是使用现有行或引发错误 - MVC5 - ManytoMany Using ApplicationUser Adds new Rows instead of using Existing Ones / Or Throws Error 添加新实体不会链接到现有的相关实体,而是创建一个新实体 - Adding new entity doesn't link to existing related entity, but creates new one instead 使用外键发布新实体会导致创建另一个外部实体而不是引用现有实体 - Posting a new entity with foreign key causes creating another foreign entity instead of referencing existing one 实体框架中间表-使用现有的不相关ID插入新记录吗? - Entity Framework intermediate table - insert new record using existing unrelated ids? 更新现有孩子而不是添加新孩子 - Updating existing child instead of adding new one ASP.NET 核心 - 如何使用实体框架更新现有记录并同时插入新记录 - ASP.NET Core - How to update existing record and insert a new one at the same time using Entity Framework 实体框架添加现有外键 - Entity Framework Adds existing foreign key 可以使用相关实体(而不是相关ID)使用OData创建实体吗? - Can one create an entity with OData using related entities (not the related IDs)? 在Linq To SQL中从现有实体创建和插入新实体 - Creating and Inserting new entity from Existing One in Linq To Sql 实体框架为导航属性添加新记录 - Entity Framework adds new record for navigation property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM