0

我有以下关系: 表关系

以及以下类(仅相关属性):

  1. Affair: (AffairID, Title, ... , AffairsGroups)
  2. 组:(GroupID,名称,...,事务组)
  3. 事务组(AffairID、GroupID、事务、组)

我想向数据库中添加一条新的 Affair 记录,该记录也将添加到 AffairsGroups 表中(使用已知 GroupID)我已经尝试过以下方式:

private Boolean addAffairToDatabase(Affair affair)
    {
        AffairDal affairContext = new AffairDal();
        affairContext.Affairs.Add(affair);
        affairContext.SaveChanges();

        AffairsGroupDal affairsGroupContext = new AffairsGroupDal();
        affairsGroupContext.AffairsGroups.Add(new AffairsGroup{ AffairID = affair.AffairID , GroupID = user.GroupID});
        affairsGroupContext.SaveChanges();
        return true;
    }

但我得到以下异常:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AffairsGroups_dbo.Affairs_AffairID". The conflict occurred in database "NuixTest.DAL.AffairsGroupDal", table "dbo.Affairs", column 'AffairID'.

这些是我的上下文类: 1。

public class AffairDal: DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Affair>().ToTable("Affairs");
    }
    public DbSet<Affair> Affairs { get; set; }
}

2.

public class AffairsGroupDal: DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<AffairsGroup>().ToTable("AffairsGroups");
    }
    public DbSet<AffairsGroup> AffairsGroups { get; set; }
}

我究竟做错了什么?谢谢

4

1 回答 1

0

如果你AffairsGroups在同一个数据库中,你应该把它们的 DbSet 放在同一个 DbContext 中。

此外,遵循Entity Framework Code First Conventions是一种很好的做法。这样就不需要添加各种 Attributes 或 Fluent API

你的AffairsGroups有一个相当简单的多对多关系: aGroup有零个或多个Affairs,每个都Affair可以是零个或多个的成员Groups

请参阅配置多对多

class Affair
{
    public int Id {set; set;}

    // an Affair can be member of many Groups:
    public virtual ICollection<Group> Groups {get; set;}
    ... 
}

classs Group
{
    public int Id {set; set;}

    // a Group can have many Affairs:
    public virtual ICollection<Affair> Affairs {get; set;}
    ... 
}

class MyDbContext : DbContext
{
    public DbSet<Affair> Affairs {get; set;}
    public DbSet<Group> Groups {get; set;}
}

实体框架将使用表名事务和组,如果你真的想命名它们Affair并且Group没有终止s,请考虑为此使用流利的API。

Entity Framework 会自动检测多对多关系并创建第三张表(AffairsGroups),如果您不喜欢默认名称,请再次使用 fluent API。

好消息是,在您对多对多的正常处理中,您不需要第三张表。选择一个事务并将组添加到其中,或者选择一个组并将事务添加到它。

using (var myDbContext = ...)
{
    // add an affair that is not in a Group yet:
    var affair1 = myDbContext.Affairs.Add(new Affair()
    {
        Groups = new List<Group>(); // not in any group yet
    });

    // add a Group that has affair1 in it:
    var group1 = myDbContext.Groups.Add(new Group()
    {
        Affairs = new List<Affair>()
        {   // only one member: affair1:
            affair1,
        },
    });
    MyDbContext.SaveChanges();

    // as a test: retrieve affair1 again and see that it suddenly has a Group in it:
    var retrievedAffair = myDbContext.Affairs
        .Where(affair => affair.Id == affair1.Id)
        .Single();
     Debug.Assert(retrievedAffair.Groups.Any());
     Group groupOfAffair = retrievedAffair.Groups.First();
     Debug.Assert(groupOfAffair.Id == group1.Id);
}

因此,在不使用事务组表的情况下,您可以添加带或不带组的事务,或者带或不带事务的组。您不需要中间表或使用此表的连接。

// get all Affairs that are in a Group with Name:
IEnumerable<Affair> affairs = myDbContext.Groups
    .Where(group => group.Name == Name)
    .Select(group => group.Affairs);
于 2017-07-13T15:08:45.577 回答