ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.2k

Error when make relationship between two tables one to many

Nov 18 2018 5:57 PM
Problem
The relationship from 'SalesFooter.SaleHeaders' to 'SalesHeader.SalesFooters' with foreign key properties {'SalesOrderNo' : int} cannot target the primary key {'SalesOrderNo' : int, 'SalesType' : int, 'SalesYear' : int, 'BranchCode' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.  
Details
I work in visual studio 2017 asp.net EntityFramework core 2.1 when i make relationship between two models SalesHeader and SalesFooter
I get error in relations why and how to solve 
SalesHeader Keys : SalesOrderNo , SalesYear ,BranchCode, SalesType(composit keys)
SalesFooter Keys :SalesOrderNo , SalesYear ,BranchCode, SalesType,,SalesLineNo (composit keys)
 
  1. SampleData  
  2.   
  3. SalesHeader Table  
  4. SalesOrderNo  SalesYear  BranchCode SalesType  CustomerID  
  5. 50              2018        1          1          20  
  6. SalesFooter Table  
  7. SalesOrderNo SalesLineNo  SalesYear  BranchCode SalesType  ItemCode  
  8. 50                1          2018        1          1          1001  
  9. 50                2          2018        1          1          1002  
  10. 50                3          2018        1          1          1003  
  1. public class SalesHeader    
  2.     {  
  3.         public int SalesOrderNo { getset; }  
  4.         public int SalesYear { getset; }  
  5.         public int BranchCode { getset; }      
  6.         public int SalesType { getset; }    
  7.         [Required]    
  8.         [DataType(DataType.Date)]    
  9.         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]    
  10.         public DateTime SalesDate { getset; }    
  11.         public ICollection SalesFooters { getset; }    
  12.         [DataType("decimal(18 ,2")]    
  13.         public decimal TotalPrice { getset; }    
  14. }    
  15.  public class SalesFooter    
  16.     {  
  17.         public int SalesOrderNo { getset; }    
  18.              
  19.          public int SalesYear { getset; }    
  20.              
  21.          public int BranchCode { getset; }    
  22.            
  23.          public int SalesType { getset; }    
  24.             
  25.         public int SalesLineNo { getset; }    
  26.         [Required]    
  27.         [DataType(DataType.Date)]    
  28.         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]    
  29.         public DateTime SalesDate { getset; }    
  30.         [DataType("decimal(18 ,2")]    
  31.         public decimal Quantity { getset; }    
  32.         [DataType("decimal(18 ,2")]    
  33.         public decimal UnitPrice { getset; }    
  34.         [DataType("decimal(18 ,2")]    
  35.         public decimal Total { getset; }    
  36.         [StringLength(100)]    
  37.         public string Notes { getset; }    
  38.         public SalesHeader SaleHeaders{ getset; }    
  39.     }    
  40. protected override void OnModelCreating(ModelBuilder modelBuilder)    
  41.         {    
  42.             base.OnModelCreating(modelBuilder);  
  43.             modelBuilder.Entity()    
  44.                 .HasKey(t => new { t.SalesOrderNo,t.SalesType,t.SalesYear,t.BranchCode });    
  45.             modelBuilder.Entity()    
  46.                 .HasKey(t => new { t.SalesOrderNo, t.SalesType, t.SalesYear, t.BranchCode,t.SalesLineNo});    
  47.             modelBuilder.Entity()    
  48.                      .HasOne(e => e.SaleHeaders)    
  49.                      .WithMany(e => e.SalesFooters)    
  50.                      .HasForeignKey(e => e.SalesOrderNo);  
  51.         }  
How to make relation between two models based on keys above ?

Answers (4)