我有以下模型:Product
类,Category
类,Color
类和Size
类;每个Product
都有Category
,Color
和Size
。
信息来自CSV文件,因此在我将产品添加到数据库之前,我会在类别,颜色和大小上选择不同的LINQ,然后将它们插入到具有EF代码的相应表格中。
在那之前,没有问题。
然后我尝试做的是,我遍历CSV文件的每一行,并为每个产品获取其类别,大小和颜色,并创建每个产品的Instance对象。
public void InsertProduct(Product product)
{
using (var context = new ExamContext())
{
ICategoryDao categoryDao = new CategoryDao();
IColorDao colorDao = new ColorDao();
ISizeDao sizeDao = new SizeDao();
var category = categoryDao.GetCategoryByName(product.Category.CategoryName);
var color = colorDao.GetColorByName(product.Color.ColorName);
var size = sizeDao.GetSizeByName(product.Size.SizeName);
var categoryEntity = new CategoryEntity()
{
Id = category.Id,
CategoryName = category.CategoryName
};
var colorEntity = new ColorEntity()
{
Id = color.Id,
ColorName = color.ColorName
};
var sizeEntity = new SizeEntity()
{
Id = size.Id,
SizeName = size.SizeName
};
ProductEntity productEntity = new ProductEntity();
productEntity.ActionPrice = product.ActionPrice;
productEntity.ArticleNumber = product.ArticleNumber;
productEntity.Category = categoryEntity;
productEntity.Color = colorEntity;
productEntity.Size = sizeEntity;
productEntity.DeliveryTime = product.DeliveryTime;
productEntity.Description = product.Description;
productEntity.Key = product.Key;
productEntity.Price = product.Price;
productEntity.ActionPrice = product.ActionPrice;
productEntity.Q1 = product.Q1;
//Had issues with mapper and navigation attributes
//var entity = Mapper.Map<Product, ProductEntity>(product);
context.ProductEntities.Add(productEntity);
context.SaveChanges();
// update business object with new id
//product.Id = entity.Id;
}
}
然后在保存更改中,我得到了一个UNIQUE KEY Exception,这是预期的,因为类别名称,颜色名称和大小名称是他们自己的表上的唯一键。 (不在产品表本身上)。
如何在不尝试插入类别,颜色和尺寸的情况下插入产品?
public class SizeEntity
{
public int Id { get; set; }
[Index("SizeName", IsUnique = true)]
[MaxLength(100)]
public string SizeName { get; set; }
}
public class ColorEntity
{
public int Id { get; set; }
[Index("ColorName", IsUnique = true)]
[MaxLength(100)]
public string ColorName { get; set; }
}
public class CategoryEntity
{
public int Id { get; set; }
[Index("CategoryName", IsUnique = true)]
[MaxLength(100)]
public string CategoryName { get; set; }
}
请注意,ProductDao对象获取一个Product(来自业务对象层),实际上不是一个Product Entity(来自数据访问层),这就是为什么我在该方法上进行某种手动映射(对象实例创建)的原因,这是出问题的地方。
答案 0 :(得分:2)
如果您不想创建重复项,则需要从数据库中获取每个实体,然后再将它们分配给产品实体,例如:
def main ():
#open gradebook
gradebook_file = open ('gradebook.txt', 'r')
#define name
name = gradebook_file.readline ()
while name != '':
grade = float (gradebook_file.readline ())
name = name.rstrip ('\n')
print ('Name:', name)
print ('Grade:', grade)
name = gradebook_file.readline ()
#append numbers in gradebook
data = []
for lines in gradebook_file:
data.append (grade)
#define min and max values
_min = min (grade)
_max = max (grade)
#print contents
print ('The minimum average grade was a(n) ' + str (_min) + '.')
print ('The maximum average grade was a(n) ' + str (_max) + '.')
#close the gradebook
gradebook_file.close ()
#call main
main ()
假设每个实体都有var categoryEntity = context.CategoryEntities.Find(category.Id);
if(categoryEntity == null)
{
categoryEntity = new CategoryEntity()
{
Id = category.Id,
CategoryName = category.CategoryName
};
context.CategoryEntities.Add(categoryEntity);
}
// same approach for other entities
作为主键,您需要在调用Id
方法之前将这些实体添加到上下文中。
您必须知道,如果您尝试执行批量插入,这种方法效率非常低。我的建议是只执行一次Find
,而不是在添加每个单个实体之后。