【发布时间】:2019-05-29 07:11:32
【问题描述】:
我有实体和相关的 DTO,它们用于将数据从 DB(实体框架 6)传输到我的 web api 的不同层。代码如下:
public class Dish
{
public int DishId { get; set; }
[Required]
[Index(IsUnique = true)]
[StringLength(400)]
public string Name { get; set; }
public string Description { get; set; }
public double PreparationTime { get; set; }
public string Photo { get; set; }
public virtual ICollection<Category> Categories { get; set; } = new List<Category>();
public virtual ICollection<Ingredient> Ingredients { get; set; } = new List<Ingredient>();
public virtual ICollection<IngredientAmount> IngredientAmounts { get; set; } = new List<IngredientAmount>();
public virtual ICollection<RecipeUnit> RecipeUnits { get; set; } = new List<RecipeUnit>();
}
public class DishDetailedDTO: IDishDTO
{
public int DishId { get; set; }
[Required]
[StringLength(400)]
public string Name { get; set; }
public string Description { get; set; }
public double PreparationTime { get; set; }
public string Photo { get; set; }
public List<CategoryDTO> Categories { get; set; }
public List<IngredientDetailedDTO> Ingredients { get; set; }
public List<IngredientAmountDTO> IngredientAmounts { get; set; }
public List<RecipeUnitDTO> RecipeUnits { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
[Required]
[Index(IsUnique = true)]
[StringLength(20)]
public string Name { get; set; }
public virtual ICollection<Dish> Dishes { get; set; } = new List<Dish>();
}
public class CategoryDTO: ICategoryDTO
{
public int CategoryId { get; set; }
[Required]
[StringLength(20)]
public string Name { get; set; }
}
当我尝试添加新的 Dish 对象时,问题就出现了,该对象的导航属性(比如说类别)已经存在于数据库中。一般来说,我从某个地方(前端)获取 DishDTO,然后使用 automapper 将其映射到 Dish,然后将此 Dish 添加到上下文中。以下是小规模问题的再现:
static void Main(string[] args)
{
Mapper.Initialize(x=>
{
x.CreateMap<Dish, DishDTO>();
x.CreateMap<Dish, DishDetailedDTO>();
x.CreateMap<Dish, DishAvailableIngredientsDTO>();
x.CreateMap<DishDTO, Dish>();
x.CreateMap<DishDetailedDTO, Dish>();
x.CreateMap<DishAvailableIngredientsDTO, Dish>();
x.CreateMap<Category, CategoryDTO>();
x.CreateMap<CategoryDTO, Category>();
x.CreateMap<Ingredient, IngredientDetailedDTO>();
x.CreateMap<Ingredient, IngredientDTO>();
x.CreateMap<IngredientDTO, Ingredient>();
x.CreateMap<IngredientDetailedDTO, Ingredient>();
x.CreateMap<IngredientAmount, IngredientAmountDTO>();
x.CreateMap<IngredientAmountDTO, IngredientAmount>();
x.CreateMap<RecipeUnit, RecipeUnitDTO>();
x.CreateMap<RecipeUnitDTO, RecipeUnit>();
});
var context = new EFDbContext();
var dish = new DishDetailedDTO()
{
Name = "SomeName",
Categories = new List<CategoryDTO>
{
new CategoryDTO()
{
Name = "Breakfast"
}
}
};
context.Dishes.Add(Mapper.Map<Dish>(dish));
context.SaveChanges();
Console.ReadLine();
}
那我有异常
SqlException: Cannot insert duplicate key row in object 'dbo.Categories' with unique index 'IX_Name'. The duplicate key value is (Breakfast).
The statement has been terminated.
【问题讨论】:
-
一道菜可以有多个分类?所以基本上你在菜和类别之间有一对多的关系?
-
@FakharAhmadRasul 是的
-
您无法将所有类别的映射菜肴直接添加到数据库集中。从映射的待添加菜品中排除导航属性并添加/排除相关类别,或者将相关类别的实体状态设置为其实际状态。
标签: c# entity-framework asp.net-web-api automapper dto