0
null 참조 오류없이 전체 목록이나 루프를 첨부하려면 어떻게합니까?엔티티가 기존 ID를 사용하는 대신 새 ID를 추가합니다.
엔티티 클래스
내가 카테고리를 연결하려고했습니다CheckBoxList
IList<ListItem> prodCat = new List<ListItem>();
foreach (ListItem item in cblCategory.Items)
{
if (item.Selected)
{
prodCat.Add(item);
}
}
에서
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; }
}
목록 추가 신제품 - 의견을 참조
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();
}
'새 제품에 오류 메시지 추가'오류 메시지의 의미는 무엇입니까? –
"System.Collections.Generic.IList"에 System.Collections.Generic.IList "형식을 변환 할 수 없습니다. –
실제 문제가 100 % 확실하지 않습니다. 입니다. 제목은 EF가 기존 개체 대신 새 개체를 만들었다 고 명시합니다. 그러나 나중에 참조 오류없이 새 객체를 만드는 방법을 묻습니다. 귀하의 질문은 분명하지 않습니다. 당신은 정교 할 수 있습니까? – sbecker