I have 3 classes that I want to relate from many to many with EF and we have this:
public class ClsPermisos: ComponenteBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Byte Codigo { get; set; }
public ICollection<ClsPermisoModuloPerfil> PermisoModuloPerfil { get; set; }
public class ClsModulosPerfiles
{
public Int64 Codigo { get; set; }
public Byte CodigoPerfil { get; set; }
public Byte CodigoModulo { get; set; }
public ClsPerfil Perfil { get; set; }
public ClsModulos Modulo { get; set; }
public ICollection<ClsPermisoModuloPerfil> PermisoModuloPerfil { get; set; }
public class ClsPermisoModuloPerfil
{
public Int64 CodigoModuloPerfil { get; set; }
public Byte CodigoPermiso { get; set; }
public Int64 Codigo { get; set; }
public ClsPermisos Permiso { get; set; }
public ClsModulosPerfiles ModuloPerfil { get; set; }
}
modelBuilder.Entity () .HasKey (mp = > new {mp.CodigoModuloPerfil, mp.CodigoPermiso});
modelBuilder.Entity<ClsPermisoModuloPerfil>().Property(mp => mp.Codigo)
.HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
modelBuilder.Entity<ClsPermisoModuloPerfil>()
.HasRequired(mp => mp.Permiso)
.WithMany(p => p.PermisoModuloPerfil)
.HasForeignKey(mp => mp.CodigoPermiso);
modelBuilder.Entity<ClsPermisoModuloPerfil>()
.HasRequired(mp => mp.ModuloPerfil)
.WithMany(m => m.PermisoModuloPerfil)
.HasForeignKey(mp => mp.CodigoModuloPerfil);
When trying to send the changes I get the following error and I do not know what I'm doing wrong.
ClsPermisoModuloPerfil_ModuloPerfil_Target_ClsPermisoModuloPerfil_ModuloPerfil_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.