c# - EF4 mapping one-to-many -


Please help find the error.

Error Text: One or more validation errors were detected during model generation. Because all the qualities in the dependent role are non-futile, the main role should have a plurality of 1.

  // File.cs public class file {public entry ID (get; set;} public string name {get} set;} public iconging & lt; Sp & gt; sps {get ; Set;}} // Special Class SP {Public Ent ID (Get; Set;} Public Ink File ID {Get; Set;} Public File File {get} Set;}}  
< P> mapping code

  // mapping file class file: EntityTypeConfiguration & lt; file & gt; {public file () {ToTable ("file", "dbo"); HasKey (x = & Gt; x ;); Property (x = & gt; x.Id) .HasColumnName ("ID"); property (x = & gt; x.Name) .HasColumnName ("NAME");}} // Mapping Spa Class SP: Entity type configuration & lt; Sp & gt; {public (SP) {ToTable ("Sp", "dbo"); HasKey (x => x.Id); Property (x = & gt; x.Id) .HasColumnName ("ID"); Property (x = & gt; x.FileId) .HasColumnName ("FILE_ID"); // The place of error isooptional (d = & gt; d.file) .mind (d = & gt ; D.S.A.). Hwjqqqi (D => Dfile ID) .WillCascadeOnDelete (wrong); }}  

You have an alternative foreign key that maps to a non-salient property That is contradictory, you have to go one way or the other.

Change HasOptional to HasRequired which means every Sp should have an associated file .

Class SP: EntityTypeConfiguration & lt; Sp & gt; {Public SP (ToTable ("Sp", "dbo"); HasKey (x => x.Id); Property (x = & gt; x.Id) .HasColumnName ("ID"); Property (x = & gt; x.FileId) .HasColumnName ("FILE_ID"); // Error HasRequired (d = & gt; d.File) status Many (d = & gt; dsps.) .eeringqi (d = & gt; d .file ID) .WillCascadeOnDelete (wrong); }}

If it is not so, then you get the fileID nullable

  public class SP {Public Entity}; Set; } Public int? Get File ID { Set; } Public file file {get; Set; }}  

SP will not be recorded with any file .


Comments