Here is my Entity-Model
[Table("WP_Schraenke")]
public class Cabinet {
[Column("Id")]
public int Id { get; set; }
[Column("Standort")]
public string Location { get; set; }
[Column("Nummer")]
public int Number { get; set; }
[Column("MonteurNr")]
public string PersonelNumber { get; set; }
public Employee Employee { get; set; }
[Column("Status")]
public CabinetState State { get; set; }
}
PersonelNumber is a foreignkey defined like
modelBuilder.Entity<Cabinet>()
.HasOne(e => e.Employee)
.WithMany()
.HasPrincipalKey(e => e.PersonelNumber)
.HasForeignKey(e => e.PersonelNumber);
I attach and save a entity on this way:
public int Save(CabinetDto cabinet) {
var ctx = this.ContextProviderService.GetE3kContext();
var entity = new Cabinet {Id = cabinet.Id};
ctx.Cabinets.Attach(entity);
entity.Location = cabinet.Location;
entity.Number = cabinet.Number;
entity.PersonelNumber = cabinet.Employee?.PersonelNumber;
ctx.SaveChanges();
return entity.Id;
}
This is working, but when I set entity.PersonelNumber to null (all other values are unchanged), SaveChanges() isn't saving this to the database.