实体框架7 AddRange()未添加外部实体

本文关键字:实体 添加 外部 框架 AddRange | 更新日期: 2023-09-27 18:29:23

我已经使用EF Code First一段时间了,但这是我第一次使用EF7。

我有以下模型类,其中Venue有一对多关系要展示:

public class Show
{
    public int Id { get; set; }
    public Venue Venue { get; set; }
    //...
}
public class Venue
{
    public int Id { get; set; }
    public string Name {get; set; }
    //...
    public List<Show> Shows { get; set; }
}

我这样设置DBContext:

public class NettlesContext : DbContext
{
    public DbSet<Show> Shows { get; set; }
    public DbSet<Venue> Venues { get; set; } 
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        new ShowConfiguration(builder.Entity<Show>());
        new ImageConfiguration(builder.Entity<Image>());
    }
}
public class ShowConfiguration
{
    public ShowConfiguration(EntityTypeBuilder<Show> builder)
    {
        builder.Property(p => p.Id).IsRequired();
        builder.Property(p => p.Title).IsRequired();
    } 
}
public class VenueConfiguration
{
    public VenueConfiguration(EntityTypeBuilder<Venue> builder)
    {
        builder.Property(p => p.Id).IsRequired();
        builder.Property(p => p.Name).IsRequired();
    }
}

然后在一些启动代码中,我初始化数据库,如下所示:

    private static void AddShows(NettlesContext db)
    {
        var shows = new List<Show>()
        {
            new Show()
            {
                Title = "Portland Country Dance Community Contra Dance",
                Venue = new Venue()
                {
                    Name = "Fulton Community Center",
                },
            },
            new Show()
            {
                Title = "Portland Roadhouse Contra Dance",
                Venue = new Venue()
                {
                    Name = "Milwaukie Community Club",
                },
            },
        };
        db.Shows.AddRange(shows);
        db.SaveChanges();
    }

Shows表已正确初始化,只是VenueId为null。会场的桌子上全是空的。

怎么回事?

实体框架7 AddRange()未添加外部实体

DbSet.Add还有第二个参数。

Add(TEntity entity, GraphBehavior behavior = GraphBehavior.IncludeDependents)

尽管默认为IncludeDependents(也称为子实体),但EF7对Add()的行为并没有将Venue标识为Show的子实体。在OnModelCreating中,您需要指定VenueShow之间的关系。请参阅EF7文档中的关系。

示例:

modelBuilder.Entity<Venue>(entityBuilder =>
{
    entityBuilder
        .HasMany(v => v.Shows)
        .WithOne(s => s.Venue)
        .HasForeignKey(s => s.VenueId);
});

尽管如此,你仍然需要打电话。添加Venue的新实例,因为Show不是Venue的附属(子)。

private static void AddShows(NettlesContext db)
{
    var fulton = new Venue()
            {
                Name = "Fulton Community Center",
            };
    var club = new Venue()
            {
                Name = "Milwaukie Community Club",
            };
    db.Venues.Add(fulton);
    db.Venues.Add(club);
    var shows = new List<Show>()
    {
        new Show()
        {
            Title = "Portland Country Dance Community Contra Dance",
            Venue = fulton,
        },
        new Show()
        {
            Title = "Portland Roadhouse Contra Dance",
            Venue = club
        },
    };
    context.Shows.AddRange(shows);
}

值得注意的是:.Add()的这种行为一直是EF7 RC1中混乱的根源,其行为可能在EF7 RC2中发生变化。看见https://github.com/aspnet/EntityFramework/pull/4132

您需要将您的模型更新为此

public class Venue
{
  public int Id { get; set; }
  public string Name {get; set; }
  //...
  public List<Show> Shows { get; set; }
 }
public class Show
{
  public int Id { get; set; }
  public int VenueId { get; set; }
  [ForeignKey("VenueId")]
  public Venue Venue { get; set; }
  //...
}

ForegnKey属性用于定义两个表之间关系的foriegn键