我使用的是Code First,我正在尝试添加2个共享相同(facet)对象的记录。EF尝试添加相同的共享对象两次,因此我得到了Violation of PRIMARY KEY constraint错误。考虑一下这个..。
XML
<RecipeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Recipes>
<Recipe author="Mr. Cook" createdDate="08/30/2018" description="Cheesesteak with mushrooms" name="Mushroom Cheesesteak" recipe_id="13">
<Facet Taxonomy_id="1" name="Lunch" />
<Facet Taxonomy_id="1" name="Dinner" />
<Facet Taxonomy_id="2" name="American" />
</Recipe>
<Recipe author="Jane Doe" createdDate="10/01/2018" description="Vegan Hotdog" name="Vegan Hotdog" recipe_id="9">
<Facet Taxonomy_id="1" name="Snack" />
<Facet Taxonomy_id="1" name="Lunch" /> <!-- This is breaking EF! -->
<Facet Taxonomy_id="2" name="Vegetarian" />
</Recipe>
</Recipes>
</RecipeSet>配方和方面具有多对多的关系。
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只保存一次唯一的面?
发布于 2018-09-21 03:32:35
我在这里找到了一些建议Entity Framework 6 inserting duplicate values。
我必须首先在数据库中保存唯一的面。然后,当我将xml映射到配方时,删除映射的facet并从数据库中分配创建的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);https://stackoverflow.com/questions/52414672
复制相似问题