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?
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.