Usuário com melhor resposta
ENTITY FRAMEWORK - RELACIONAMENTO ENTRE 2 TABELAS COM CHAVE COMPOSTA - HELP

Pergunta
-
Bom dia,
Tenho as seguintes tabelas mapeadas no EF:
Tabela PRODUTO com chave composta -->
[Table("Produto")]
public partial class Produto
{
[Key, Column(Order = 0)]
public int ProCodigo { get; set; }
[Key, Column(Order = 1)]
public int EMPCODIGO { get; set; }
public string ProDescricao { get; set; }
public int UniCodigo { get; set; }
public int PlaConCodigo { get; set; }
//Definindo as chaves estrangeiras (ligações) da tabela
[ForeignKey("UniCodigo")]
public virtual Unidade unidade { get; set; }
[ForeignKey("PlaConCodigo")]
public virtual PlanoContas planoContas { get; set; }
}e a tabela dependente EQUIPAMENTO:
[Table("equipamento")]
public partial class Equipamento
{
[Key]
public int EquCodigo { get; set; }
public int ProCodigo { get; set; }
public int EmpCodigo { get; set; }
public int SisCodigo { get; set; }
public string EquIdentificacao { get; set; }
public string EquNome { get; set; }
public System.DateTime EquAquisicao { get; set; }
public System.DateTime EquGarantia { get; set; }
public float EquValorAqui { get; set; }
public int EquVidaUtilAno { get; set; }
//Definindo as chaves estrangeiras (ligações) da tabela
[ForeignKey("ProCodigo")]
public virtual Produto produto { get; set; }
[ForeignKey("EmpCodigo")]
public virtual Produto empCodigo { get; set; }
[ForeignKey("SisCodigo")]
public virtual Sistema sistema { get; set; }
}A tabela EQUIPAMENTO se relaciona com a tabela PRODUTO através das chaves ProCodigo e EmpCodigo, que são chaves composta na tabela PRODUTO.
Quando tento o relacionamento acima, rodando a aplicação, me é retornado a seguinte mensagem de erro:
One or more validation errors were detected during model generation:\r\n\r\nEquipamento_empCodigo_Target_Equipamento_empCodigo_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.\r\nEquipamento_produto_Target_Equipamento_produto_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
Sabem me dizer onde estou errando?
Agradeço aos que puderem me ajudar nisto.
Obrigado!
Buscando o conhecimento
Respostas
-
Resolvido o problema.
Abaixo, as linhas que sofreram modificações estão em negrito.
Vejam como ficou:
[Table("equipamento")]
public partial class Equipamento
{
[Key]
public int EquCodigo { get; set; }
[ForeignKey("Produto"), Column(Order = 0)]
public int ProCodigo { get; set; }
[ForeignKey("Produto"), Column(Order = 1)]
public int EmpCodigo { get; set; }
public int SisCodigo { get; set; }
public int EquCodigoControle { get; set; }
public int ClaCodigo { get; set; }
public string EquIdentificacao { get; set; }
public string EquNome { get; set; }
public System.DateTime EquAquisicao { get; set; }
public System.DateTime EquGarantia { get; set; }
public float EquValorAqui { get; set; }
public int EquVidaUtilAno { get; set; }
public int EquHsTrabAno { get; set; }
public float EquVrResidual { get; set; }
public float EquTxJurosAnual { get; set; }
public string EquCertificado { get; set; }
public System.DateTime EquValidadeCer { get; set; }
public int EquLocAtual { get; set; }
public float EquValorLocacao { get; set; }
public int EquStatus { get; set; }
public string EquNF { get; set; }
public int EmpCodigoFornecedor { get; set; }
public string EquTipoLocacao { get; set; }
public string EquObs { get; set; }
public float EquDepreciacao { get; set; }
public System.DateTime EquInicioDepreciacao { get; set; }
public int EquKmInicial { get; set; }
public int EquHoraInicial { get; set; }
public int EquLocAnterior { get; set; }
public string EquAlugada { get; set; }
public string EQUIRFID { get; set; }
public string EQUIFOTO { get; set; }
//Definindo as chaves estrangeiras (ligações) da tabela
public virtual Produto Produto { get; set; } //Solução para o caso de relacionamentos com tabelas com chaves compostas
[ForeignKey("SisCodigo")]
public virtual Sistema sistema { get; set; }
}É isso aí!
Buscando o conhecimento