C# 实体框架6.x代码第一主键约束错误保存多对多字段
我首先使用代码,并尝试添加共享同一facet对象的两条记录。EF尝试两次添加相同的共享对象,因此我得到了违反主键约束的错误。考虑这个… XML 配方和方面有多对多的关系C# 实体框架6.x代码第一主键约束错误保存多对多字段,c#,entity-framework-6,many-to-many,automapper,C#,Entity Framework 6,Many To Many,Automapper,我首先使用代码,并尝试添加共享同一facet对象的两条记录。EF尝试两次添加相同的共享对象,因此我得到了违反主键约束的错误。考虑这个… XML 配方和方面有多对多的关系 public class Recipe { [Key] public int Recipe_id { get; set; } public string Author { get; set; } public DateTime CreatedDate { get; set; } pub
public class Recipe
{
[Key]
public int Recipe_id { get; set; }
public string Author { get; set; }
public DateTime CreatedDate { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public virtual ICollection<Facet> Facets { get; set; }
}
//Facet needs to have a unique taxonomy id and name combination
public class Facet
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Facet_id { get; set; }
[Key]
[Column(Order = 0)]
public int Taxonomy_id { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(500)]
public string Name { get; set; }
public virtual ICollection<Recipe> Recipes { get; set; }
}
public class RecipeContext : DbContext
{
public RecipeContext() : base("name=RecipeContextConn")
{
Database.SetInitializer<RecipeContext>(new CreateDatabaseIfNotExists<RecipeContext>());
}
public DbSet<Recipe> Recipe { get; set; }
public DbSet<Facet> Facet { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
// Automapper configuration
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<RecipeElement, Recipe>()
.ForMember(dest => dest.Facets, opt => opt.MapFrom(src => src.Facet));
cfg.CreateMap<FacetElement, Facet>();
});
Mapper = config.CreateMapper();
我的代码在Foreach循环后保存时中断
foreach (var recipeElement in fullRecipeSet.Recipes.Recipe)
{
// Using Automapper to map from one object to another
Recipe recipeDto = Mapper.Map<Recipe>(recipeElement);
ctx.Recipe.Add(recipeDto);
}
ctx.SaveChanges();
如何告诉EF只保存一次唯一的方面?我在这里找到了一些建议 我必须先在DB中保存独特的方面。然后,当我将xml映射到配方时,删除映射的facet并从数据库中分配创建的facet。这确保所有配方共享相同的精确刻面对象
Recipe recipeDto = MapInitializer.Mapper.Map<Recipe>(recipeElement);
var facs = recipeDto.Facets;
// Null out the facets that gets mapped from the xml.
recipeDto.Facets = new List<Facet>();
// Reassign facet from the db. Otherwise, trying to save the recipe with the
// facets that was mapped from the xml will cause duplicate facts trying to insert.
foreach (var f in facs)
{
var dbFacet = ctx.Facet.Where(x => x.Taxonomy_id == f.Taxonomy_id && x.Name == f.Name).First();
recipeDto.Facets.Add(dbFacet);
}
ctx.Recipe.Add(recipeDto);
几年后来到这里,但谢谢你。这救了我。我在拯救许多重复的孩子方面遇到了问题。谢谢
Recipe recipeDto = MapInitializer.Mapper.Map<Recipe>(recipeElement);
var facs = recipeDto.Facets;
// Null out the facets that gets mapped from the xml.
recipeDto.Facets = new List<Facet>();
// Reassign facet from the db. Otherwise, trying to save the recipe with the
// facets that was mapped from the xml will cause duplicate facts trying to insert.
foreach (var f in facs)
{
var dbFacet = ctx.Facet.Where(x => x.Taxonomy_id == f.Taxonomy_id && x.Name == f.Name).First();
recipeDto.Facets.Add(dbFacet);
}
ctx.Recipe.Add(recipeDto);