By the description of your question, it seems to me that you want to set up a relationship of 1:N
, where a Empresa
can have N
Projeto
if a Projeto
can only have 1
Empresa
then you could have the following mapping.:
public class Projeto
{
[Key]
public int ProjetoId { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public byte[] Versao { get; set; }
public int EmpresaId { get; set; }
[ForeignKey("EmpresaId")]
public virtual Empresa Empresa { get;set; }
}
public class Empresa
{
[Key]
public Int32 EmpresaId { get;set; }
public String Nome { get;set; }
public String Fone { get;set; }
public String Contato { get;set; }
public String Email { get;set; }
public virtual ICollection<Projeto> Projetos { get;set; }
}
You can omit the EmpresaId
column in Projeto
as follows, but I do not recommend it.
public class Projeto
{
[Key]
public int ProjetoId { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public byte[] Versao { get; set; }
public virtual Empresa Empresa { get;set; }
}
public class Empresa
{
[Key]
public Int32 EmpresaId { get;set; }
public String Nome { get;set; }
public String Fone { get;set; }
public String Contato { get;set; }
public String Email { get;set; }
public virtual ICollection<Projeto> Projetos { get;set; }
}
I do not recommend the following fact: If you have EmpresaId
of Empresa
and want to update Projeto
, you'll have to load Empresa
into Contexto
, or Attach
incomplete or searching with Find
.
// Attach do objeto incompleto
var empresa = new Empresa { EmpresaId = empresaId };
context.Empresas.Attach(empresa);
context.Entry(empresa).State = EntityState.Unchanged;
projeto.Empresa = empresa;
context.SaveChanges();
// Consultar Objeto
var empresa = context.Empresas.Find(empresaId);
projeto.Empresa = empresa;
context.SaveChanges();
// Setar o EmpresaId no Projeto
projeto.EmpresaId = empresaId;
context.SaveChanges();
But if you really need a N:M
relationship, you are not forced to create an extra Entity.
public class Projeto
{
[Key]
public int ProjetoId { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public byte[] Versao { get; set; }
public virtual ICollection<Empresa> Empresas { get;set; }
}
public class Empresa
{
[Key]
public Int32 EmpresaId { get;set; }
public String Nome { get;set; }
public String Fone { get;set; }
public String Contato { get;set; }
public String Email { get;set; }
public virtual ICollection<Projeto> Projetos { get;set; }
}
In the example above, the EF will create the ProjetoEmpresa
table in the Database, having a composite key with ProjetoId
and EmpresaId
.
However, if the connection between Projeto
and Empresa
adds some information, such as the Contract number, in this case it is interpresante to add a new entity.
public class Projeto
{
[Key]
public int ProjetoId { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public byte[] Versao { get; set; }
public virtual ICollection<ProjetoEmpresa> Empresas { get;set; }
}
public class Empresa
{
[Key]
public Int32 EmpresaId { get;set; }
public String Nome { get;set; }
public String Fone { get;set; }
public String Contato { get;set; }
public String Email { get;set; }
public virtual ICollection<ProjetoEmpresa> Projetos { get;set; }
}
public class ProjetoEmpresa
{
[Key, Column(Order = 1)]
public int ProjetoId { get; set; }
[Key, Column(Order = 2)]
public int EmpresaId { get; set; }
public string Contrato { get; set; }
[ForeignKey("ProjetoId")]
public virtual Projeto Projeto { get;set; }
[ForeignKey("EmpresaId")]
public virtual Empresa Empresa { get;set; }
}
Again, you may omit the fields EmpresaId
and ProjetoId
in ProjetoEmpresa
, but do not recommend.
Finally, I do not see a gain in having Chave Simples Auto Incremental
in the ProjetoEmpresa
table, remember, it will usually only be used as a bridge between Projeto
and Empresa
so it's interesting to have a compound key that enforces this integrity.
If you choose to have a Chave Simples Auto Incremental
, do not forget to create a Índice Único
as suggested by @CiganoMorrisonMendez.