Entity Framework C# .NET -Entitäten mit mehreren TabellenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Entity Framework C# .NET -Entitäten mit mehreren Tabellen

Post by Anonymous »

Ich versuche, den Kontext mehrerer Entitäten im Entitäts -Framework von C# .NET zu erstellen.
Ich habe die folgenden Entitäten: < /p>
public class User {
public int id { get; set; } // id int4 primary key,
public string name { get; set; } // name varchar (50) not null unique,
public string? first_name { get; set; } // first_name varchar (50) null,
public string? last_name { get; set; } // last_name varchar (50) null,
public string password_hash { get; set; } // password_hash text not null,
public List\ roles { get; set; }
}
public class Role {
public int id { get; set; } // id int4 primary key,
public string name { get; set; } // name varchar (50) not null unique,
public string? description { get; set; } // description varchar (255),
public List\ users { get; set; }
public List\ locations_zones { get; set; }
}
public class LocationZone {
public int id { get; set; } // id int4 primary key,
public string name { get; set; } // id_location_zone varchar(50) not null,
public string? description { get; set; } // description varchar(255) null,
public List\ locations { get; set; }
public List\ roles { get; set; }
public List\ permissions { get; set; }

}
public class Permission {
public int id { get; set; } // id int4 primary key,
public string name { get; set; } // name varchar (50) not null unique,
public string? description { get; set; } // description varchar (255),
public List\ locations_zones { get; set; }
}
public class RolePermission {
public int id_role { get; set; } // id_role int4,
public int id_location_zone { get; set; } // id_location_zone int4,
public int id_permission { get; set; } // id_permission int4,
public List\ roles { get; set; }
public List\ location_zones { get; set; }
public List\ permissions { get; set; }
}
< /code>
Für den Kontext habe ich den folgenden Code: < /p>
model_builder.Entity(entity => { entity.HasKey(_ => new { _.id }); entity.ToTable("users"); entity.HasMany(_ => _.roles).WithMany(_ => _.users).UsingEntity( "users_roles", j => j.HasOne().WithMany().HasForeignKey(_ => _.id_role), j => j.HasOne().WithMany().HasForeignKey(_ => _.id_user)); }); model_builder.Entity(entity => { entity.HasKey(_ => new { _.id }); entity.ToTable("roles"); entity.HasMany(_ => _.locations_zones).WithMany(_ => _.roles).UsingEntity( "roles_permissions", j => { j.ToTable("roles_permissions"); j.HasKey(rp => new { rp.id_role, rp.id_location_zone, rp.id_permission }); j.HasOne().WithMany().HasForeignKey(_ => _.id_location_zone); j.HasOne().WithMany().HasForeignKey(_ => _.id_role); }); }); model_builder.Entity(entity => { entity.HasKey(_ => new { _.id }); entity.ToTable("location_zones"); });
< /code>
Die Tabellen haben eine Beziehung von Benutzern N: m mit Rollen der Tabellenrollen. /> Vielen Dank im Voraus < /p < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post