Hallo zusammen,
ich habe seit ein paar Tagen ein Problem wo ich trotz Recherche nicht weiterkomme.
Ich habe ein EF Modell und die entsprechenden Klassen auf Basis einer SQL-Datenbank aufgebaut.
Wenn ich nun einen neuen Datensatz hinzufügen möchte erhalte ich folgenden Fehler:
Fehler |
System.InvalidOperationException: "The value of 'Documents.ID' is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known." |
Ich kann nicht wirklich ausmachen welche Tabelle das Problem verursacht - vermute aber die Tabelle Matchcodes.....
Die Klassen zu den beiden Entitäten "Matchcodes" und "Documents" sehen wie folgt aus:
Matchcodes
public class Matchcodes
{
[Key]
public int ID { get; set; }
public string Matchcode { get; set; }
public int DocumentsID { get; set; }
public virtual Documents Documents { get; set; }
}
Documents
public class Documents
{
private ILazyLoader LazyLoader { get; set; }
public Documents()
{
_matchcodes = new HashSet<Matchcodes>();
_documentNotices = new HashSet<DocumentNotices>();
}
private Documents(ILazyLoader lazyloader)
{
LazyLoader = lazyloader;
}
[Key]
public int ID { get; set; }
public string DocumentName { get; set; }
public int DocumentType { get; set; }
public decimal DocumentSize { get; set; }
public byte[] DocumentBinary { get; set; }
public string DocumentPath { get; set; }
public string DocumentOrigin { get; set; }
public int FolderID { get; set; }
public int Revision { get; set; }
public DateTimeOffset? CreationDate { get; set; }
public DateTime? DocumentDate { get; set; }
public int OriginalDocument { get; set; }
public bool ForAll { get; set; }
public int UserID { get; set; }
[NotMapped]
public bool Concat { get; set; }
[NotMapped]
public bool DoOCR { get; set; }
public FileTypes FileType { get; set; }
public DocumentsFullText DocumentsFullText { get; set; }
private ICollection<Matchcodes> _matchcodes;
public ICollection<Matchcodes> Matchcodes
{
get => LazyLoader.Load(this, ref _matchcodes);
set => _matchcodes = value;
}
private ICollection<DocumentNotices> _documentNotices;
public ICollection<DocumentNotices> DocumentNotices
{
get => LazyLoader.Load(this, ref _documentNotices);
set => _documentNotices = value;
}
[NotMapped]
public string FileTypeAsString
{
get
{
return App.ModelContext.FileTypes.Where(x => x.ID == DocumentType).FirstOrDefault().FileType;
}
}
}
Im Kontext selbst habe ich via Fluent API folgendes gesetzt:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>()
.HasOne(u => u.ProgramUsage)
.WithOne()
.HasForeignKey<Users>(x => x.ID);
modelBuilder.Entity<Documents>()
.HasOne(d => d.FileType)
.WithOne()
.HasForeignKey<Documents>(x => x.ID);
modelBuilder.Entity<Documents>()
.HasMany<Matchcodes>(x => x.Matchcodes)
.WithOne()
.HasForeignKey(x=>x.ID);
modelBuilder.Entity<Documents>()
.HasOne(d => d.DocumentsFullText)
.WithOne()
.HasForeignKey<Documents>(x => x.ID);
}
}
Ich habe schon ein paar Änderungen im Kontext durchgeführt und ein paar Dokus dazu gelesen aber ich vermute mal, dass ich irgendetwas grandios Missverstanden habe.
Habt ihr einen Denkanstoß?
Gruß,
D.
P.S.:
Falls Abt das lesen sollte - DateTime? sollte nicht verwendet werden - ist mir dank dir bekannt und wird auch noch geändert - aber zunächst möchte ich das andere Problem lösen :-)