CribAd
CribAd

Reputation: 504

EF Core Add() method removes database records

This looks strange, but it happens: when I try to add a new records to my DB using EF Core, the method first removes some other records.

I have 3 database tables with Users, Organizations and Positions, and 1 table that links the data, the UserOrganizationPosition table. Every combination is possible but should be unique.

Models

public partial class User
{
    public string UserID { get; set; }
    public string UserName { get; set; }
    public virtual List<UserOrganizationPosition> _uop { get; set; }
}

public partial class Organization
{
    public string OrganizationID { get; set; }
    public string OrganizationName { get; set; }
    public virtual List<UserOrganizationPosition> _uop { get; set; }
}

public partial class Position
{
    public string PositionID { get; set; }
    public string PositionName { get; set; }
    public virtual List<UserOrganizationPosition> _uop { get; set; }
}

public partial class UserOrganizationPosition 
{
    public string UserID { get; set; }
    public virtual User _user { get; set; }
    public string OrganizationID { get; set; }
    public virtual Organiaztion _organization { get; set; }
    public string PositionID { get; set; }
    public virtual Position _position { get; set; }
}

WebDbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<UserOrganizationPosition>(entity =>
    {
        entity.HasKey(uop => new { uop.UserID, uop.OrganizationID, uop.PositionID});
    });

    modelBuilder.Entity<UserOrganizationPosition>()
        .HasOne(uop => uop._user)
        .WithMany(u => u._uop)
        .HasForeignKey(uop => uop.UserID);
    modelBuilder.Entity<UserOrganizationPosition>()
        .HasOne(uop => uop._organization)
        .WithMany(o => o._uop)
        .HasForeignKey(uop => uop.OrganizationID);
    modelBuilder.Entity<UserOrganizationPosition>()
        .HasOne(uop => uop._position)
        .WithMany(p => p._uop)
        .HasForeignKey(uop => uop.PositionID);
}

My Method

In my method I try to add a new UserOrganizationPosition:

WebDbContext.Add(
    new UserOrganizationPosition{
        UserID = "aaa",
        OrganizationID = "BBB",
        PositionID = "222"
    }
);

Before this call, I check if the combination does not already exist and I know that the used ID's do exist in the other tables.

My Database is populated with a lot of UserOrganizationPosition's and when I add a new one, some other UserOrganizationPosition's are deleted. Does anyone know how this can happen?

I'm not 100% sure, but it looks like it doesn't accept multiple combinations with 2 out f 3 the same combinations like this:

aaa BBB 222

ccc DDD 333 <- full different, it's okay

aaa DDD 444 <- is unique with aaa and DDD equal to some other links, it's okay

aaa BBB 444 <- matches 2 out of 3 parts of the first combination, will be removed

Again: I'm not 100% sure about this!

Upvotes: 1

Views: 124

Answers (1)

Steve Py
Steve Py

Reputation: 34908

Consider using the entity references rather than setting FKs directly. Retrieve the User, Organization, and Positions from the context and add their references to a new combination entity and call SaveChanges. This should ensure that all referenced entity relationships are set properly if you later inspect those entities. For EF Core I recommend using shadow properties for the FK declarations to avoid exposing two separate representations of the same relationship. (UserOrganizationPosition.PositionId and UserOrganizationPosition.Position.PositionId)

Upvotes: 0

Related Questions