首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Entity Framework 6.x Code First主键约束错误保存多对多字段

Entity Framework 6.x Code First主键约束错误保存多对多字段
EN

Stack Overflow用户
提问于 2018-09-20 05:52:24
回答 1查看 111关注 0票数 0

我使用的是Code First,我正在尝试添加2个共享相同(facet)对象的记录。EF尝试添加相同的共享对象两次,因此我得到了Violation of PRIMARY KEY constraint错误。考虑一下这个..。

XML

代码语言:javascript
复制
<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>

配方和方面具有多对多的关系。

代码语言:javascript
复制
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循环后保存时,代码会崩溃。

代码语言:javascript
复制
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只保存一次唯一的面?

EN

回答 1

Stack Overflow用户

发布于 2018-09-21 03:32:35

我在这里找到了一些建议Entity Framework 6 inserting duplicate values

我必须首先在数据库中保存唯一的面。然后,当我将xml映射到配方时,删除映射的facet并从数据库中分配创建的facet。这确保了所有配方都共享相同的facet对象

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52414672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档