Ok, I found out that I made a small mistake and thanks to Elio.Batista I realized it.
For validation purposes I set some default values to some properties. Among these properties there are GsmOperator
and Machine
.
But, instead of setting the GsmOperatorId
property, I mistakenly set the RTMS_GsmOperator
property which is the E-F
entity. And because I set some default value, its not null and E-F
recognizes it as a NEW entity and tries to INSERT
it.
So, during an EF INSERT
, the objects ForeignKey linked entity should be null.
My wrong code:
//Notice that this is partial entity class of EF edmx file
public partial class RTMS_PackageApplication
{
public RTMS_PackageApplication()
{
this.RTMS_GsmOperator = new RTMS_GsmOperator();
this.RTMS_Machine = new RTMS_Machine();
}
}
Now, I changed and corrected to:
//Notice that this is partial entity class of EF edmx file
public partial class RTMS_PackageApplication
{
public RTMS_PackageApplication()
{
this.GsmOperatorId = 0; //As Default value
this.MachineId = 0; //As Default value
}
}