Reputation: 5740
we want to migrate our project from Database-First to Code-First. For this task i used the Code First from database Generator from Visual Studio. I have some tables with concatenated PKs and FKs. I cannot change them to one simple "ID", because there is a legacy application that needs them this way. The EF version is 6.1.3
When i start my programm, I'm getting the following error:
(1568,10) : error 3015: Problem in mapping fragments starting at lines 1568, 1583: Foreign key constraint 'tblRechnungPosition_tblAngebReches' from table tblAngebRech (RechNr, RechPosNr, CompanyID) to table tblRechnungPosition (RechNr, CompanyID, PosNr):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.
I cannot figure out what is causing the error, for me all the relations seem to be correct.
Here are the classes:
public partial class tblAngebRech
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AngebotID { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AngebPosNr { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int RechNr { get; set; }
[Key]
[Column(Order = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int RechPosNr { get; set; }
[Key]
[Column(Order = 4)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CompanyID { get; set; }
public DateTime Timestamp { get; set; }
public virtual tblAngebotPosition tblAngebotPosition { get; set; }
public virtual tblRechnungPosition tblRechnungPosition { get; set; }
}
.
public partial class tblRechnungPosition
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblRechnungPosition()
{
tblAngebReches = new HashSet<tblAngebRech>();
tblBeauftReches = new HashSet<tblBeauftRech>();
tblRechPosMitarbs = new HashSet<tblRechPosMitarb>();
}
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int RechNr { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CompanyID { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int PosNr { get; set; }
public int PosTypID { get; set; }
public int? StdKeyID { get; set; }
public double Menge { get; set; }
public double Betrag { get; set; }
[Required]
[StringLength(100)]
public string Bezeichnung { get; set; }
public DateTime Timestamp { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblAngebRech> tblAngebReches { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblBeauftRech> tblBeauftReches { get; set; }
public virtual tblPositionstyp tblPositionstyp { get; set; }
public virtual tblRechnung tblRechnung { get; set; }
public virtual tblStundenKey tblStundenKey { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblRechPosMitarb> tblRechPosMitarbs { get; set; }
And this is from protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Entity<tblRechnungPosition>()
.HasMany(e => e.tblAngebReches)
.WithRequired(e => e.tblRechnungPosition)
.HasForeignKey(e => new { e.RechNr, e.CompanyID, e.RechPosNr });
Any help would be appreciated.
Upvotes: 3
Views: 2522
Reputation: 612
Try to manually adjust the order of the FK properties in the modelbuilder to that:
modelBuilder.Entity<tblRechnungPosition>()
.HasMany(e => e.tblAngebReches)
.WithRequired(e => e.tblRechnungPosition)
.HasForeignKey(e => new { e.RechNr, e.RechPosNr, e.CompanyID });
I sent a bug issue to the EF 6.x Team at codeplex. Here is the link:https://entityframework.codeplex.com/workitem/2947
If that doesn't work. Try change the Order Number of the Column Attribute in class tblRechnungPosition to:
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int RechNr { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CompanyID { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int PosNr { get; set; }
Note: You have composite foreign keys. In both table classes the order of the properties must be the same inside the [Columns(Order = <order number>)]
.
Upvotes: 2