TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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)
SampleData
SalesHeader Table
SalesOrderNo SalesYear BranchCode SalesType CustomerID
50 2018 1 1 20
SalesFooter Table
SalesOrderNo SalesLineNo SalesYear BranchCode SalesType ItemCode
50 1 2018 1 1 1001
50 2 2018 1 1 1002
50 3 2018 1 1 1003
public
class
SalesHeader
{
public
int
SalesOrderNo {
get
;
set
; }
public
int
SalesYear {
get
;
set
; }
public
int
BranchCode {
get
;
set
; }
public
int
SalesType {
get
;
set
; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString =
"{0:yyyy-MM-dd}"
, ApplyFormatInEditMode =
true
)]
public
DateTime SalesDate {
get
;
set
; }
public
ICollection SalesFooters {
get
;
set
; }
[DataType(
"decimal(18 ,2"
)]
public
decimal
TotalPrice {
get
;
set
; }
}
public
class
SalesFooter
{
public
int
SalesOrderNo {
get
;
set
; }
public
int
SalesYear {
get
;
set
; }
public
int
BranchCode {
get
;
set
; }
public
int
SalesType {
get
;
set
; }
public
int
SalesLineNo {
get
;
set
; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString =
"{0:yyyy-MM-dd}"
, ApplyFormatInEditMode =
true
)]
public
DateTime SalesDate {
get
;
set
; }
[DataType(
"decimal(18 ,2"
)]
public
decimal
Quantity {
get
;
set
; }
[DataType(
"decimal(18 ,2"
)]
public
decimal
UnitPrice {
get
;
set
; }
[DataType(
"decimal(18 ,2"
)]
public
decimal
Total {
get
;
set
; }
[StringLength(100)]
public
string
Notes {
get
;
set
; }
public
SalesHeader SaleHeaders{
get
;
set
; }
}
protected
override
void
OnModelCreating(ModelBuilder modelBuilder)
{
base
.OnModelCreating(modelBuilder);
modelBuilder.Entity()
.HasKey(t =>
new
{ t.SalesOrderNo,t.SalesType,t.SalesYear,t.BranchCode });
modelBuilder.Entity()
.HasKey(t =>
new
{ t.SalesOrderNo, t.SalesType, t.SalesYear, t.BranchCode,t.SalesLineNo});
modelBuilder.Entity()
.HasOne(e => e.SaleHeaders)
.WithMany(e => e.SalesFooters)
.HasForeignKey(e => e.SalesOrderNo);
}
How to make relation between two models based on keys above ?
Reply
Answers (
4
)
MVC4 fetch binary images on the basis of ID without EF
Add ASP.NET MVC5 Identity Authentication to an existing APP