EF core: cannot insert object with foreign key using ID properties (only navigation properties work)

c# entity-framework-core

Question

I'm using Entity Framework Core to create my (code first) database model.
In the model I have these classes:

[Table("groups")]
public class Group {
    public int? Id { get; set; }
    [JsonIgnore]
    public virtual ICollection<Membership> Members { get; set; }
}

[Table("identities")]
public class Identity {
    public int? Id { get; set; }
    [JsonIgnore]
    public virtual ICollection<Membership> MemberOf { get; set; }
}

[Table("memberships")]
public partial class Membership {
    [Column("group_id")]
    public int GroupId { get; set; }
    [Column("identity_id")]
    public int IdentityId { get; set; }
    [JsonIgnore]
    public virtual Group Group { get; set; }
    [JsonIgnore]
    public virtual Identity Identity{ get; set; }
}

Membership is a many-to-many relationship between Group and Identity.
I have configured the relationship using the fluent API like this:

protected override void OnModelCreating(ModelBuilder modelBuilder) {
    var entity = modelBuilder.Entity<Membership>();
    entity.HasKey(e => new { 
        e.GroupId, 
        e.IdentityId }
    ).HasName("PK_identity_memberships");
    entity.HasOne(d => d.Group)
        .WithMany(p => p.Members)
        .HasForeignKey(d => d.GroupId)
        .HasConstraintName("memberships_group_id_fkey");
    entity.HasOne(d => d.Identity)
        .WithMany(p => p.MemberOf)
        .HasForeignKey(d => d.IdentityId)
        .HasConstraintName("memberships_identity_id_fkey");
}

My problem is that if I try to insert a Membership in the DB setting GroupId and IdentityId with valid values, I get this error:

Npgsql.PostgresException: 23503: insert or update on table "memberships" violates foreign key constraint "memberships_group_id_fkey"

To insert a Membership object, I need to set the Group and Identity navigation properties, and then everything works fine.

Is there a way to insert new objects without using the navigation properties?

1
2
4/11/2018 2:46:18 PM

Popular Answer

I've finally found the problem. To reproduce (at least with NpgSql):

using (var db = new TestContext()) {

    // This works:
    db.Groups.Add(new Group {
        Id = 42,
    });
    db.Identities.Add(new Identity {
        Id = 666,
    });
    db.SaveChanges();

    db.Memberships.Add(new Membership { 
        GroupId = 42,
        IdentityId = 666
    });
    db.SaveChanges();

    // this explodes...
    db.Groups.Add(new Group {
        Id = 0,
    });
    db.Identities.Add(new Identity {
        Id = 0,
    });
    db.SaveChanges();

    db.Memberships.Add(new Membership { 
        GroupId = 0,
        IdentityId = 0
    });
    db.SaveChanges(); // exception here!

}

So, the problem was that I was using 0 as an ID for the objects I had created.
Using a non-zero integer, even negative, seems to work instead.
This might be a bug in NpgSql, I suppose, since 0 seems to be a valid value for an ID (since I can insert records manually in the DB).

1
4/12/2018 9:44:55 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow