I don’t agree with that nomenclature of tabela
even because the Entity Framework is agnostic about the database, so it doesn’t necessarily operate in the relational model, just on top of tables. It goes beyond that. But back to the answer:
Having this statement:
public class tabela1
{
public Guid tabela1Id {get;set;}
//campos...
public ICollection <tabela2> tabela2 {get;set;}
}
You need to have this:
public class tabela2
{
[Key]
public Guid tabela2id { get; set; }
// Anote também a FK, assim:
public Guid tabela1id { get; set; }
public virtual tabela1 tabela1 { get; set }
}
This generates FK with the name tabela1id
.
Now, if you want another name for FK, use the [ForeignKey]
as follows:
public class tabela2
{
[Key]
public Guid tabela2id { get; set; }
// Anote também a FK, assim:
public Guid MinhaFKCujoNomeEuEscolhi { get; set; }
[ForeignKey("MinhaFKCujoNomeEuEscolhi")]
public virtual tabela1 tabela1 { get; set }
}
Or else:
public class tabela2
{
[Key]
public Guid tabela2id { get; set; }
// Anote também a FK, assim:
[ForeignKey("MinhaPropriedadeDeNavegacaoCujoNomeEuEscolhi")]
public Guid tabela1id { get; set; }
public virtual tabela1 MinhaPropriedadeDeNavegacaoCujoNomeEuEscolhi { get; set }
}
This nomenclature used only as an example here, and this is possible to be done using Fluent API ?
– Aesir
Possible, but I find the Fluent API more complicated to use.
– Leonel Sanches da Silva