What is the correct way to declare the following class structure and get the respective Entityframework behavior?

Asked

Viewed 158 times

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 omitted some properties to not get too extensive.

The structure consists of Resales containing Companies and these contain your customers. Companies are customers of Resellers was called thus.

Yet what I’m demonstrating in the structure is that by the class Revenda has the list of registered users for the Revenda, in Empresa has her list of Users and Cliente the same, his user list.

Pretty soon I got:

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, for example in UsuarioRevenda which has a navigation property of Companies that he has access to.

Via Migrations, soon at the helm Add-Migration, I’m getting a message saying that, Revenda, for example, you cannot have a property of the type ICollection<UsuarioRevenda>, because of the attribute InverseProperty pointing to a property originally declared in the class Usuario, the property Revenda.

The message is as follows:

Resale: Fromrole: Navigationproperty 'Revenda' is not Valid. Type 'Usuariorevenda' of Fromrole 'Revenda_usuarios_target' in Associationtype 'Revenda_usuarios' must Exactly match with the type 'Usuario' on which this Navigationproperty is declared on.

All user classes are linked in the same database table, and are then broken down by the field Discriminator, automatically, by Entityframework.

What is the correct way to declare this type of structure for the Entityframework and using ASP.Net Identity?

  • 2

    I find it unnecessary to use InverseProperty. This decoration is good to do when the class names are different for some particular reason, which is not the case. Have you tried taking the attribute and performing the migration procedure?

  • 1

    It creates by a behavior limitation of the Entity Framework (I think I gave another answer to a question about you).

1 answer

1

Like the comment of fellow Gypsy, the attribution of the attribute InverseProperty

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

Just remove attribute from classes Revenda, Empresa and Cliente, in the methods refer to the error returned by Entityframework, that the problem has been solved.

That is to say:

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; }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.