简体   繁体   中英

Entity Framework 6.x Code First PRIMARY KEY constraint error saving Many to Many field

I am using Code First and I'm trying to add 2 records that share the same (facet) object. EF is trying to add the same shared object twice so I get the Violation of PRIMARY KEY constraint error. Consider this..

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>

Recipe and Facets have a many to many relationship.

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();

My code breaks when I save after the Foreach loop.

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();

How do I tell EF to only save unique Facets only once?

I found some suggestions here Entity Framework 6 inserting duplicate values .

I had to save the unique facets in the DB first. Then, when I map the xml to the recipe, remove the mapped facets and assign the created facets from database. This ensures that all recipes share the same exact facet object

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM