What is the correct way to declare the following class structure and get its behavior from EntityFramework?

2

I have the following class structure:

public class Revenda
{
    [InverseProperty("Revenda")]
    public virtual ICollection<UsuarioRevenda> Usuarios { get; set; }
}

public class Empresa
{
    [InverseProperty("Empresa")]
    public virtual ICollection<UsuarioEmpresa> Usuarios { get; set; }
}

public class Cliente
{
    [InverseProperty("Cliente")]
    public virtual ICollection<UsuarioCliente> Usuarios { get; set; }
}

I've omitted some properties to not be too long.

The structure consists of Resales containing Business and these contain your clients. Companies are resellers customers got called that way.

However, what I'm showing in the structure is that by class Revenda has the list of registered Users for Revenda , in Empresa has its Users list and Cliente the same, users.

Soon, then I have:

public class Usuario : IdentityUser {
    [Required]
    [ForeignKey("Revenda")]
    public virtual int RevendaId { get; set; }
    public virtual Revenda Revenda { get; set; }

    [Required]
    [ForeignKey("Empresa")]
    public virtual int? EmpresaId { get; set; }
    public virtual Empresa Empresa { get; set; }

    [Required]
    [ForeignKey("Cliente")]
    public virtual int? ClienteId { get; set; }
    public virtual Cliente Cliente { get; set; }
}

Other user profiles:

public class UsuarioRevenda : Usuario { 
    [Required]
    [ForeignKey("Revenda")]
    public override int RevendaId { get; set; }
    public override Revenda Revenda { get; set; }
}

public class UsuarioEmpresa : Usuario { 
    [Required]
    [ForeignKey("Empresa")]
    public override int? EmpresaId { get; set; }
    public override Empresa Empresa { get; set; }
}

public class UsuarioCliente : Usuario { 
    [Required]
    [ForeignKey("Cliente")]
    public override int? ClienteId { get; set; }
    public override Cliente Cliente { get; set; }
}

Some properties of each class have also been omitted here, such as UsuarioRevenda , which has a Companies navigation property that it has access to.

I'm getting a message saying, Migrations , for example, can not have a property of type Add-Migration , because of the Revenda attribute that points to a property originally declared in class ICollection<UsuarioRevenda> , property InverseProperty .

The message is as follows:

  

Reseller: FromRole: NavigationProperty 'Reseller' is not valid. Type 'UserRevenda' of FromRole 'Reseller_Usuarios_Target' in AssociationType 'Reseller_Users' must exactly match with the type 'User' on which this NavigationProperty is declared on.

All user classes are linked in the same database table and are then automatically

asked by anonymous 30.09.2014 / 23:30

1 answer

1

As the companion comment Gypsy, assigning the attribute InverseProperty

  

"It's good to do when class names are different for some particular reason, which is not the case."

Just remove the attribute of the Revenda , Empresa , and Cliente classes in methods that refer to the error returned by EntityFramework , that solved the problem.

That is:

public class Revenda {
    public virtual ICollection<UsuarioRevenda> Usuarios { get; set; }
}

public class Empresa {
    public virtual ICollection<UsuarioEmpresa> Usuarios { get; set; }
}

public class Cliente {
    public virtual ICollection<UsuarioCliente> Usuarios { get; set; }
}
    
01.10.2014 / 03:23