I need help with identity and roles
these are my classes that all extends the user class:
c#
public class Admin : Utilisateur
{
}
public class Demandeur : Utilisateur
{
[Column(TypeName = "decimal(10, 10)")]
public decimal Lattitude { get; set; }
[Column(TypeName = "decimal(10, 10)")]
public decimal Longitude { get; set; }
public bool IsMagasinier { get; set; }
public ICollection<Voyage> Voyages { get; set; } = [];
}
public class Magasinier : Utilisateur
{
public Entrepot Entrepot { get; set; } = null!;
public string EntrepotId { get; set; } = string.Empty;
public ICollection<Voyage> Voyages { get; set; } = [];
}
public class Transporteur : Utilisateur
{
public Vehicule? Vehicule { get; set; }
public string? VehiculeId { get; set; } = string.Empty;
public bool ExterneDuService { get; set; } = false;
public List<Voyage> Voyages { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
public class Utilisateur: IdentityUser<Guid>
{
public string Nom { get; set; } = string.Empty;
public string Prenom { get; set; } = string.Empty;
}
c#
public class Admin : Utilisateur
{
}
public class Demandeur : Utilisateur
{
[Column(TypeName = "decimal(10, 10)")]
public decimal Lattitude { get; set; }
[Column(TypeName = "decimal(10, 10)")]
public decimal Longitude { get; set; }
public bool IsMagasinier { get; set; }
public ICollection<Voyage> Voyages { get; set; } = [];
}
public class Magasinier : Utilisateur
{
public Entrepot Entrepot { get; set; } = null!;
public string EntrepotId { get; set; } = string.Empty;
public ICollection<Voyage> Voyages { get; set; } = [];
}
public class Transporteur : Utilisateur
{
public Vehicule? Vehicule { get; set; }
public string? VehiculeId { get; set; } = string.Empty;
public bool ExterneDuService { get; set; } = false;
public List<Voyage> Voyages { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
public class Utilisateur: IdentityUser<Guid>
{
public string Nom { get; set; } = string.Empty;
public string Prenom { get; set; } = string.Empty;
}
9 Replies
can someone please help me?
this is my class diagram
c#
namespace backend.Data
{
public class ApplicationDBContext : IdentityDbContext<IdentityUser>
{
public ApplicationDBContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
{
}
public DbSet<Entrepot> Entrepot { get; set; }
public DbSet<Utilisateur> Utilisateur { get; set; }
public DbSet<Admin> Admin { get; set; }
public DbSet<Magasinier> Magasinier { get; set; }
public DbSet<Transporteur> Transporteur { get; set; }
public DbSet<Demandeur> Demandeur { get; set; }
p
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<TransporteurVoyage>().HasKey(t => new { t.TransporteurId, t.VoyageId });
List<IdentityRole> roles = new List<IdentityRole>{
new IdentityRole{
Name = "Admin",
NormalizedName = "ADMIN"
},
new IdentityRole{
Name = "Magasinier",
NormalizedName = "MAGASINIER"
},
new IdentityRole{
Name="Demandeur",
NormalizedName = "DEMANDEUR"
},
new IdentityRole{
Name="Transporteur",
NormalizedName = "TRANSPORTEUR"
},
};
builder.Entity<IdentityRole>().HasData(roles);
builder.Entity<Transporteur>()
.HasMany(t => t.Voyages)
.WithMany(v => v.Transporteurs)
.UsingEntity<TransporteurVoyage>(
l => l.HasOne<Voyage>().WithMany().HasForeignKey(e => e.VoyageId).OnDelete(DeleteBehavior.NoAction),
r => r.HasOne<Transporteur>().WithMany().HasForeignKey(e => e.TransporteurId).OnDelete(DeleteBehavior.NoAction)
);
}
}
}
c#
namespace backend.Data
{
public class ApplicationDBContext : IdentityDbContext<IdentityUser>
{
public ApplicationDBContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
{
}
public DbSet<Entrepot> Entrepot { get; set; }
public DbSet<Utilisateur> Utilisateur { get; set; }
public DbSet<Admin> Admin { get; set; }
public DbSet<Magasinier> Magasinier { get; set; }
public DbSet<Transporteur> Transporteur { get; set; }
public DbSet<Demandeur> Demandeur { get; set; }
p
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<TransporteurVoyage>().HasKey(t => new { t.TransporteurId, t.VoyageId });
List<IdentityRole> roles = new List<IdentityRole>{
new IdentityRole{
Name = "Admin",
NormalizedName = "ADMIN"
},
new IdentityRole{
Name = "Magasinier",
NormalizedName = "MAGASINIER"
},
new IdentityRole{
Name="Demandeur",
NormalizedName = "DEMANDEUR"
},
new IdentityRole{
Name="Transporteur",
NormalizedName = "TRANSPORTEUR"
},
};
builder.Entity<IdentityRole>().HasData(roles);
builder.Entity<Transporteur>()
.HasMany(t => t.Voyages)
.WithMany(v => v.Transporteurs)
.UsingEntity<TransporteurVoyage>(
l => l.HasOne<Voyage>().WithMany().HasForeignKey(e => e.VoyageId).OnDelete(DeleteBehavior.NoAction),
r => r.HasOne<Transporteur>().WithMany().HasForeignKey(e => e.TransporteurId).OnDelete(DeleteBehavior.NoAction)
);
}
}
}
You must pass your usermodel to the IdentityContext
Not identities baseclass
wdym?
Look at your context inheritance
You are passing IdentityUser
That needs to be your user class
The Demandeur, Admin, Magasinier and the transporteur class are all userModel
I'm not sure you can do that
You normally make one class and differentiate between users based on roles
Understood
ty
Ok