Though the underlying tables, classes have correct data type for foreign key, still getting error message The type of property on entity does not match the type of property on entity in the referential constraint.
It is automatically mapping to string type field.
How do I force to map to what I declare as foreign key?
public class Proposal : IEntity<long>, IEntity
{
public enum RiskLevelAccess
{
Deny=0,View = 1, Edit = 2
};
public Proposal()
{
this.Notes = new List<ProposalNote>();
this.Resources = new List<ProposalResource>();
this.WorkAllocations = new List<ProposalWorkAllocation>();
this.Revisions = new List<string>();
this.ProposalMethodofExecutions = new List<ProposalMethodofExecution>();
this.ProposalProjectPhases = new List<ProposalProjectPhase>();
this.ProposalPractices = new List<ProposalPractice>();
this.ProposalRiskChallenges = new List<ProposalRiskChallenge>();
this.ProposalServiceTypes = new List<ProposalServiceType>();
}
[Key]
public long ProposalId { get; set; }
public string ProposalNumber { get; set; }
}
public class ProposalMethodofExecution : IEntity<int?>,IEntity
{
[Key]
public int? PK { get; set; }
public long ProposalId { get; set; }
[ForeignKey("ProposalId")]
public virtual Proposal proposal { get; set; }
public byte? MethodofExecutionId { get; set; }
[ForeignKey("MethodofExecutionId")]
public virtual MethodofExecution methodofexecution { get; set; }
public string Description { get; set; }
public string Other { get; set; }
public bool Selected { get; set; }
public int? Key { get { return this.PK; } }
}
Detailed error:
ProposalMethodofExecution_proposal_Target_ProposalMethodofExecution_proposal_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property
'ProposalId' on entity 'ProposalMethodofExecution' does not match the type of property 'ProposalNumber' on entity 'Proposal' in the referential constraint 'ProposalMethodofExecution_proposal'.
Proposal_ProposalMethodofExecutions_Source_Proposal_ProposalMethodofExecutions_Target: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property
'ProposalId' on entity 'ProposalMethodofExecution' does not match the type of property 'ProposalNumber' on entity 'Proposal' in the referential constraint 'Proposal_ProposalMethodofExecutions'.
Regards,
Raj
Remember to click Mark as Answer on the post that helps to others.
Based on your code, I create a sample using the following code, it seems that everything works well, you could check it:
public class SchoolContext:DbContext
{
public SchoolContext() : base("ConnectionString")
{
}
public DbSet<Proposal> Proposals { get; set; }
public DbSet<ProposalMethodofExecution> Props { get; set; }
}
public interface IEntity
{
int ID { get; set; }
string Name { get; set; }
}
public interface IEntity<T>
{
int IID { get; set; }
string IName { get; set; }
}
public class Proposal : IEntity<string>, IEntity
{
public enum RiskLevelAccess
{
Deny = 0, View = 1, Edit = 2
};
[Key]
public long ProposalId { get; set; }
public string ProposalNumber { get; set; }
public int IID { get; set; }
public string IName { get; set; }
public int ID { get; set; }
public string Name { get; set; }
}
public class ProposalMethodofExecution : IEntity<string>, IEntity
{
[Key]
public int? PK { get; set; }
public long ProposalId { get; set; }
[ForeignKey("ProposalId")]
public virtual Proposal proposal { get; set; }
public string Description { get; set; }
public string Other { get; set; }
public bool Selected { get; set; }
public int? Key { get { return this.PK; } }
public int IID { get; set; }
public string IName { get; set; }
public int ID { get; set; }
public string Name { get; set; }
}
So, I suppose perhaps the issue is related to the IEntity interface, you could check it.
Best regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
one thing I have noted in your code is IEntity typed to string where as mine is typed to int. Not sure is that causing issue!
I also change it to int or long type, they are still works well on my side. You can check it.
Best regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Participant
1041 Points
651 Posts
Foreign key data type int not accepted
Oct 26, 2016 03:46 PM|krisrajz|LINK
Though the underlying tables, classes have correct data type for foreign key, still getting error message The type of property on entity does not match the type of property on entity in the referential constraint.
It is automatically mapping to string type field.
How do I force to map to what I declare as foreign key?
Detailed error:
ProposalMethodofExecution_proposal_Target_ProposalMethodofExecution_proposal_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ProposalId' on entity 'ProposalMethodofExecution' does not match the type of property 'ProposalNumber' on entity 'Proposal' in the referential constraint 'ProposalMethodofExecution_proposal'.
Proposal_ProposalMethodofExecutions_Source_Proposal_ProposalMethodofExecutions_Target: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ProposalId' on entity 'ProposalMethodofExecution' does not match the type of property 'ProposalNumber' on entity 'Proposal' in the referential constraint 'Proposal_ProposalMethodofExecutions'.
Raj
Remember to click Mark as Answer on the post that helps to others.
All-Star
45479 Points
7008 Posts
Microsoft
Re: Foreign key data type int not accepted
Oct 27, 2016 08:50 AM|Zhi Lv - MSFT|LINK
Hi krisrajz,
Based on your code, I create a sample using the following code, it seems that everything works well, you could check it:
So, I suppose perhaps the issue is related to the IEntity interface, you could check it.
Best regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Participant
1041 Points
651 Posts
Re: Foreign key data type int not accepted
Oct 27, 2016 02:01 PM|krisrajz|LINK
Thanks for your time, one thing I have noted in your code is IEntity typed to string where as mine is typed to int. Not sure is that causing issue!
Raj
Remember to click Mark as Answer on the post that helps to others.
All-Star
45479 Points
7008 Posts
Microsoft
Re: Foreign key data type int not accepted
Nov 01, 2016 07:10 AM|Zhi Lv - MSFT|LINK
Hi krisrajz,
I also change it to int or long type, they are still works well on my side. You can check it.
Best regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.