C
C#8mo ago
daminko

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;
}
No description
9 Replies
daminko
daminkoOP8mo ago
can someone please help me?
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)
);
}
}
}
this is my class diagram
daminko
daminkoOP8mo ago
No description
Pobiega
Pobiega8mo ago
You must pass your usermodel to the IdentityContext Not identities baseclass
daminko
daminkoOP8mo ago
wdym?
Pobiega
Pobiega8mo ago
Look at your context inheritance You are passing IdentityUser That needs to be your user class
daminko
daminkoOP8mo ago
The Demandeur, Admin, Magasinier and the transporteur class are all userModel
Pobiega
Pobiega8mo ago
I'm not sure you can do that You normally make one class and differentiate between users based on roles
daminko
daminkoOP8mo ago
Understood ty
owen111
owen1118mo ago
Ok
Want results from more Discord servers?
Add your server