我正在使用Code First,并且试图添加2条共享相同(构面)对象的记录。 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仅将唯一的方面保存一次?
答案 0 :(得分:0)
我在Entity Framework 6 inserting duplicate values处找到了一些建议。
我必须首先将唯一的方面保存在数据库中。然后,当我将xml映射到配方时,删除映射的构面并从数据库分配创建的构面。这样可以确保所有食谱共享相同的确切构面对象
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);