如何在多对多关系中插入或更新EF?

时间:2017-12-28 18:03:03

标签: c# mysql asp.net entity-framework entity-framework-core

我正在努力插入或更新多对多关系中的记录。问题在于交叉表。我已经使用Entity Framework Database First生成了类。即使我清除了交叉表并且我重新填充了它得到唯一约束异常。我该怎么办?

结构:

故事)1 ------ N( StoryLocation )N ------- 1(位置

生成的表格和背景

public partial class Story
{
    public Story()
    {
        StoryLocation = new HashSet<StoryLocation>();
    }

    [Column("id", TypeName = "bigint(20)")]
    public long Id { get; set; }

    [InverseProperty("Story")]
    public ICollection<StoryLocation> StoryLocation { get; set; }
}

public partial class Location
{
    public Location()
    {
        StoryLocation = new HashSet<StoryLocation>();
    }

    [Column("id", TypeName = "bigint(20)")]
    public long Id { get; set; }

    [InverseProperty("Location")]
    public ICollection<StoryLocation> StoryLocation { get; set; }
}



public partial class StoryLocation
    {
        [Column("story_id", TypeName = "bigint(20)")]
        public long StoryId { get; set; }
        [Column("location_id", TypeName = "bigint(20)")]
        public long LocationId { get; set; }

        [ForeignKey("LocationId")]
        [InverseProperty("StoryLocation")]
        public Location Location { get; set; }
        [ForeignKey("StoryId")]
        [InverseProperty("StoryLocation")]
        public Story Story { get; set; }
    }

DBContext 流利的Api:

public partial class TreasureContext : DbContext
{

    public virtual DbSet<Location> Locations { get; set; }
    public virtual DbSet<Story> Stories { get; set; }
    public virtual DbSet<StoryLocation> StoryLocations { get; set; }

    public TreasureContext(DbContextOptions options):base(options)
    {

    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
        optionsBuilder.UseMySql("Server[servername];Port=3306;
                        Database[dbname];Uid=[admin];Pwd=[pwd];");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Location>(entity =>
        {
            entity.HasIndex(e => e.ActivityId)
                .HasName("FKh3ry7cjb7l4vv0qx8d2jbcrcx");
        });



     modelBuilder.Entity<StoryLocation>(entity =>
        {
            entity.HasKey(e => new { e.LocationId, e.StoryId });

            entity.HasIndex(e => e.StoryId)
                .HasName("FK_story");

            entity.HasOne(d => d.Location)
                .WithMany(p => p.StoryLocation)
                .HasForeignKey(d => d.LocationId)
                .HasConstraintName("FK_location");

            entity.HasOne(d => d.Story)
                .WithMany(p => p.StoryLocation)
                .HasForeignKey(d => d.StoryId)
                .HasConstraintName("FK_story");
        });

    }
}

ASP MVC Action和包装器参数:

public class Relation
{
    public long storyid{get;set;}
    public List<Location>locations;
}

[HttpPost, ActionName("Attach")]
public IActionResult Attach([FromBody]Relation relation)
{
    Story targetStory=this.context.Stories.Find(relation.storyid);
    if(targetStory!=null)
    {
        this.context.StoryLocations.RemoveRange(
            from slocation in this.context.StoryLocations 
            where slocation.StoryId.Equals(targetStory.Id)
            select slocation
            );

        foreach(Location loc in relation.locations)
        {
            this.context.StoryLocations.Add(new 
            StoryLocation{Location=loc,Story=targetStory});
        }
        this.context.SaveChanges();
    }
    return RedirectToAction("Index", "Story");
}

我想将ASP MVC Action参数中收到的所有位置附加到故事中的故事中。

0 个答案:

没有答案