Two foreign keys in the same column EF

0

I'm having a question, I'm programming with .NET MVC using the Entity Framework. I currently have:

class Produto
{
    public int ProdutoId { get; set; }
    public string Nome { get; set; }
    public string Descricao { get; set; }

    public virtual UsuarioSistema CadastradoPor { get; set; }
    public virtual UsuarioVisitante CadastradoPor { get; set; }

}

The problem is: the 'RegisteredCode' field must have the FK of the UserAgent or VisitorUser.

The UserEntity Entity has many more attributes than the User Entity Entity, so they are separate.

But even if the UserVisitante is simple and limited, it can also register a product. How should I make the relationship in the Product entity?

class UsuarioVisitante : Usuario
{
    public virtual Fornecedor Fornecedor { get; set; }
    public virtual ICollection<Produto> Produtos { get; set; }
}
    
asked by anonymous 10.01.2018 / 17:53

1 answer

0

So you're confusing some things. EF with Entity, Class Table, and Column Attribute.

First:

public virtual UsuarioSistema CadastradoPor { get; set; }
public virtual UsuarioVisitante CadastradoPor { get; set; }

You can not have two Attributes with the same name in the same Class , as you can not declare the same variable twice in the same scope.

Second:

In the database you can not have a single column, [CriadoPor] with FK for the two UsuarioSistema and UsuarioVisitante . I suspect FK is for [Usuario] Table which should have a Column [Tipo] that you use to differentiate whether it is "System" or "Visitor"

    
10.01.2018 / 21:42