Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Added the real code used on the attempt with EF Annotations
Source Link
Marcelo Myara
  • 3k
  • 2
  • 32
  • 37

EDIT 1:

This is the real code that I'm using to accomplish the scenario mentioned, is the following:

public class PacientePesquisa { [Key, Column(Order = 1)] [Required(ErrorMessage = Msg_Critica_Req_IDPesquisa)] [Display(Name = "Pesquisa")] public int IDPesquisa { get; set; } [ForeignKey("IDPesquisa")] public virtual Pesquisa Pesquisa { get; set; } [Key, Column(Order = 2)] [Required(ErrorMessage = Msg_Critica_Req_NRProntuario)] [StringLength(NRProntuario_MaxLength, ErrorMessage = Msg_Critica_Tam_NRProntuario)] [Display(Name = "Prontuário")] public string NRProntuario { get; set; } [ForeignKey("NRProntuario")] public virtual Paciente Paciente { get; set; } (...) public class Pesquisa { [Key] public int IDPesquisa { get; set; } (...) public class Paciente { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public string NRProntuario { get; set; } (...) 

And when running the "add-migration", I'm receiving the following error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: System.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical. 

EDIT 1:

This is the real code that I'm using to accomplish the scenario mentioned, is the following:

public class PacientePesquisa { [Key, Column(Order = 1)] [Required(ErrorMessage = Msg_Critica_Req_IDPesquisa)] [Display(Name = "Pesquisa")] public int IDPesquisa { get; set; } [ForeignKey("IDPesquisa")] public virtual Pesquisa Pesquisa { get; set; } [Key, Column(Order = 2)] [Required(ErrorMessage = Msg_Critica_Req_NRProntuario)] [StringLength(NRProntuario_MaxLength, ErrorMessage = Msg_Critica_Tam_NRProntuario)] [Display(Name = "Prontuário")] public string NRProntuario { get; set; } [ForeignKey("NRProntuario")] public virtual Paciente Paciente { get; set; } (...) public class Pesquisa { [Key] public int IDPesquisa { get; set; } (...) public class Paciente { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public string NRProntuario { get; set; } (...) 

And when running the "add-migration", I'm receiving the following error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: System.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical. 
corrected spelling and removed non-essential comments
Source Link
webdad3
  • 9.2k
  • 31
  • 131
  • 229

I've found some similar questions here, but none were aswered with something that would work for me.

I have a project with about 60 entities with lots of cenariosscenarios, all mapped to the database by the simple use of some basic EF annotations. One cenarioscenario that I couldn't put to work, is the following:

public class Client { [Key] public int IDClient { get; set; } (...) } public class Research { [Key] public int IDReasearch { get; set; } (...) } public class ClientReaserach { [Key, Column(Order = 1)] public int IDClient { get; set; } [ForeignKey("IDClient")] public virtual Client Client { get; set; } [Key, Column(Order = 2)] public int IDResearch { get; set; } [ForeignKey("IDResearch")] public virtual Research Research { get; set; } (...) } 

Although this seems like other cenariosscenarios that worked so far, this one seems to me like a fluentAPI kind of thing (as far as I know, annotations only gives me a sub-set of EF's real capacities).

So I have some questions:

  • Can I remain using my annotations and use fluentAPI to map only the situation described above?

  • How will EF "knows" that this entities and its relationships are mapped through the fluentAPI (and not try to map them again when I add a new migration)?

  • Is it OK to rebuild all my project using fluentAPI? What should I do withh all the migrations already generated (should I just delete them)?

  • How would I accomplish the above cenarioscenario through fluentAPI?

Wow, lot's of questions. That's why any help will be very appreciated.

I've found some similar questions here, but none were aswered with something that would work for me.

I have a project with about 60 entities with lots of cenarios, all mapped to the database by the simple use of some basic EF annotations. One cenario that I couldn't put to work, is the following:

public class Client { [Key] public int IDClient { get; set; } (...) } public class Research { [Key] public int IDReasearch { get; set; } (...) } public class ClientReaserach { [Key, Column(Order = 1)] public int IDClient { get; set; } [ForeignKey("IDClient")] public virtual Client Client { get; set; } [Key, Column(Order = 2)] public int IDResearch { get; set; } [ForeignKey("IDResearch")] public virtual Research Research { get; set; } (...) } 

Although this seems like other cenarios that worked so far, this one seems to me like a fluentAPI kind of thing (as far as I know, annotations only gives me a sub-set of EF's real capacities).

So I have some questions:

  • Can I remain using my annotations and use fluentAPI to map only the situation described above?

  • How will EF "knows" that this entities and its relationships are mapped through the fluentAPI (and not try to map them again when I add a new migration)?

  • Is it OK to rebuild all my project using fluentAPI? What should I do withh all the migrations already generated (should I just delete them)?

  • How would I accomplish the above cenario through fluentAPI?

Wow, lot's of questions. That's why any help will be very appreciated.

I've found some similar questions here, but none were aswered with something that would work for me.

I have a project with about 60 entities with lots of scenarios, all mapped to the database by the simple use of some basic EF annotations. One scenario that I couldn't put to work, is the following:

public class Client { [Key] public int IDClient { get; set; } (...) } public class Research { [Key] public int IDReasearch { get; set; } (...) } public class ClientReaserach { [Key, Column(Order = 1)] public int IDClient { get; set; } [ForeignKey("IDClient")] public virtual Client Client { get; set; } [Key, Column(Order = 2)] public int IDResearch { get; set; } [ForeignKey("IDResearch")] public virtual Research Research { get; set; } (...) } 

Although this seems like other scenarios that worked so far, this one seems to me like a fluentAPI kind of thing (as far as I know, annotations only gives me a sub-set of EF's real capacities).

So I have some questions:

  • Can I remain using my annotations and use fluentAPI to map only the situation described above?

  • How will EF "knows" that this entities and its relationships are mapped through the fluentAPI (and not try to map them again when I add a new migration)?

  • Is it OK to rebuild all my project using fluentAPI? What should I do withh all the migrations already generated (should I just delete them)?

  • How would I accomplish the above scenario through fluentAPI?

Source Link
Marcelo Myara
  • 3k
  • 2
  • 32
  • 37

How to use EF fluentApi and set a class with composite key of 2 different foreign keys?

I've found some similar questions here, but none were aswered with something that would work for me.

I have a project with about 60 entities with lots of cenarios, all mapped to the database by the simple use of some basic EF annotations. One cenario that I couldn't put to work, is the following:

public class Client { [Key] public int IDClient { get; set; } (...) } public class Research { [Key] public int IDReasearch { get; set; } (...) } public class ClientReaserach { [Key, Column(Order = 1)] public int IDClient { get; set; } [ForeignKey("IDClient")] public virtual Client Client { get; set; } [Key, Column(Order = 2)] public int IDResearch { get; set; } [ForeignKey("IDResearch")] public virtual Research Research { get; set; } (...) } 

Although this seems like other cenarios that worked so far, this one seems to me like a fluentAPI kind of thing (as far as I know, annotations only gives me a sub-set of EF's real capacities).

So I have some questions:

  • Can I remain using my annotations and use fluentAPI to map only the situation described above?

  • How will EF "knows" that this entities and its relationships are mapped through the fluentAPI (and not try to map them again when I add a new migration)?

  • Is it OK to rebuild all my project using fluentAPI? What should I do withh all the migrations already generated (should I just delete them)?

  • How would I accomplish the above cenario through fluentAPI?

Wow, lot's of questions. That's why any help will be very appreciated.