add lazy loading for entities

This commit is contained in:
hydrant
2019-05-13 12:51:56 +02:00
parent d87bbd5a9c
commit ac6e4b51a9
23 changed files with 1308 additions and 28 deletions

View File

@@ -21,7 +21,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("User")] [ForeignKey("User")]
public int UserId { get; set; } public int UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
public string Reason { get; set; } public string Reason { get; set; }
public string BannedBy { get; set; } public string BannedBy { get; set; }

View File

@@ -17,7 +17,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("User")] [ForeignKey("User")]
public int UserId { get; set; } public int UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
public bool Gender { get; set; } public bool Gender { get; set; }
public byte Father { get; set; } public byte Father { get; set; }

View File

@@ -17,7 +17,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("User")] [ForeignKey("User")]
public int UserId { get; set; } public int UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
public bool Duty { get; set; } public bool Duty { get; set; }

View File

@@ -31,6 +31,6 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("FactionId")] [ForeignKey("FactionId")]
public int? FactionId { get; set; } public int? FactionId { get; set; }
public Faction Faction { get; set; } public virtual Faction Faction { get; set; }
} }
} }

View File

@@ -20,7 +20,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("Faction")] [ForeignKey("Faction")]
public int FactionId { get; set; } public int FactionId { get; set; }
public Faction Faction { get; set; } public virtual Faction Faction { get; set; }
public bool Gender { get; set; } public bool Gender { get; set; }

View File

@@ -20,7 +20,7 @@ namespace ReallifeGamemode.Server.Entities
public int Id { get; set; } public int Id { get; set; }
[ForeignKey("Faction")] [ForeignKey("Faction")]
public int FactionId { get; set; } public int FactionId { get; set; }
public Faction Faction { get; set; } public virtual Faction Faction { get; set; }
[StringLength(12)] [StringLength(12)]
public string Bic { get; set; } public string Bic { get; set; }
[StringLength(32)] [StringLength(32)]

View File

@@ -13,7 +13,7 @@ namespace ReallifeGamemode.Server.Entities
public int FactionId { get; set; } public int FactionId { get; set; }
public Faction Faction { get; set; } public virtual Faction Faction { get; set; }
public Faction GetFaction() public Faction GetFaction()
{ {

View File

@@ -15,7 +15,7 @@ namespace ReallifeGamemode.Server.Entities
{ {
[ForeignKey("Faction")] [ForeignKey("Faction")]
public int? FactionId { get; set; } public int? FactionId { get; set; }
public Faction Faction { get; set; } public virtual Faction Faction { get; set; }
public Faction GetFaction() public Faction GetFaction()
{ {

View File

@@ -13,7 +13,7 @@ namespace ReallifeGamemode.Server.Entities
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; } public int Id { get; set; }
public Group Group { get; set; } public virtual Group Group { get; set; }
public int Balance { get; set; } public int Balance { get; set; }
} }

View File

@@ -10,7 +10,7 @@ namespace ReallifeGamemode.Server.Entities
{ {
public class GroupVehicle : ServerVehicle public class GroupVehicle : ServerVehicle
{ {
public Group Group { get; set; } public virtual Group Group { get; set; }
[ForeignKey("Group")] [ForeignKey("Group")]
public int? GroupId { get; set; } public int? GroupId { get; set; }

View File

@@ -21,11 +21,11 @@ namespace ReallifeGamemode.Server.Entities.Logs
[ForeignKey("Victim")] [ForeignKey("Victim")]
public int VictimId { get; set; } public int VictimId { get; set; }
public User Victim { get; set; } public virtual User Victim { get; set; }
[ForeignKey("Killer")] [ForeignKey("Killer")]
public int? KillerId { get; set; } public int? KillerId { get; set; }
public User Killer { get; set; } public virtual User Killer { get; set; }
public float VictimPositionX { get; set; } public float VictimPositionX { get; set; }
public float VictimPositionY { get; set; } public float VictimPositionY { get; set; }

View File

@@ -14,7 +14,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("User")] [ForeignKey("User")]
public int? UserId { get; set; } public int? UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
public string Caption { get; set; } public string Caption { get; set; }
public string Content { get; set; } public string Content { get; set; }

View File

@@ -19,7 +19,6 @@ namespace ReallifeGamemode.Server.Entities
public class ShopVehicle : ServerVehicle public class ShopVehicle : ServerVehicle
{ {
[ForeignKey("Shop")] [ForeignKey("Shop")]
public int? ShopId { get; set; }
public int BusinessId { get; set; } public int BusinessId { get; set; }
public int Price { get; set; } public int Price { get; set; }

View File

@@ -44,24 +44,26 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("Character")] [ForeignKey("Character")]
public int? CharacterId { get; set; } public int? CharacterId { get; set; }
public Character Character { get; set; } public virtual Character Character { get; set; }
[ForeignKey("Ban")] [ForeignKey("Ban")]
public int? BanId { get; set; } public int? BanId { get; set; }
public Ban Ban { get; set; } public virtual Ban Ban { get; set; }
public int? FactionId { get; set; } public int? FactionId { get; set; }
public Faction Faction { get; set; } public virtual Faction Faction { get; set; }
public bool FactionLeader { get; set; } public bool FactionLeader { get; set; }
public int? FactionRankId { get; set; } public int? FactionRankId { get; set; }
public FactionRank FactionRank { get; set; } public virtual FactionRank FactionRank { get; set; }
public Group Group { get; set; } public virtual Group Group { get; set; }
public GroupRank GroupRank { get; set; } public GroupRank GroupRank { get; set; }
public virtual Job Job { get; set; }
public FactionRank GetFactionRank() public FactionRank GetFactionRank()
{ {
using (var dbContext = new DatabaseContext()) using (var dbContext = new DatabaseContext())

View File

@@ -23,7 +23,7 @@ namespace ReallifeGamemode.Server.Entities
public int Id { get; set; } public int Id { get; set; }
[ForeignKey("User")] [ForeignKey("User")]
public int UserId { get; set; } public int UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
[StringLength(12)] [StringLength(12)]
public string Bic { get; set; } public string Bic { get; set; }
[StringLength(32)] [StringLength(32)]

View File

@@ -21,7 +21,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("User")] [ForeignKey("User")]
public int UserId { get; set; } public int UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
public int Amount { get; set; } public int Amount { get; set; }
public int Slot { get; set; } public int Slot { get; set; }

View File

@@ -15,7 +15,7 @@ namespace ReallifeGamemode.Server.Entities
{ {
[ForeignKey("User")] [ForeignKey("User")]
public int UserId { get; set; } public int UserId { get; set; }
public User User { get; set; } public virtual User User { get; set; }
public override string ToString() public override string ToString()
{ {

View File

@@ -14,7 +14,7 @@ namespace ReallifeGamemode.Server.Entities
[ForeignKey("Vehicle")] [ForeignKey("Vehicle")]
public int ServerVehicleId { get; set; } public int ServerVehicleId { get; set; }
public ServerVehicle Vehicle { get; set; } public virtual ServerVehicle Vehicle { get; set; }
public int Slot { get; set; } public int Slot { get; set; }
public int ModId { get; set; } public int ModId { get; set; }

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,90 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ReallifeGamemode.Migrations
{
public partial class Jobs : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "JobId",
table: "Users",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "JobId",
table: "ServerVehicles",
nullable: true);
migrationBuilder.CreateTable(
name: "Jobs",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Jobs", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Users_JobId",
table: "Users",
column: "JobId");
migrationBuilder.CreateIndex(
name: "IX_ServerVehicles_JobId",
table: "ServerVehicles",
column: "JobId");
migrationBuilder.AddForeignKey(
name: "FK_ServerVehicles_Jobs_JobId",
table: "ServerVehicles",
column: "JobId",
principalTable: "Jobs",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Users_Jobs_JobId",
table: "Users",
column: "JobId",
principalTable: "Jobs",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ServerVehicles_Jobs_JobId",
table: "ServerVehicles");
migrationBuilder.DropForeignKey(
name: "FK_Users_Jobs_JobId",
table: "Users");
migrationBuilder.DropTable(
name: "Jobs");
migrationBuilder.DropIndex(
name: "IX_Users_JobId",
table: "Users");
migrationBuilder.DropIndex(
name: "IX_ServerVehicles_JobId",
table: "ServerVehicles");
migrationBuilder.DropColumn(
name: "JobId",
table: "Users");
migrationBuilder.DropColumn(
name: "JobId",
table: "ServerVehicles");
}
}
}

View File

@@ -420,6 +420,18 @@ namespace ReallifeGamemode.Migrations
b.ToTable("Interiors"); b.ToTable("Interiors");
}); });
modelBuilder.Entity("ReallifeGamemode.Server.Entities.Job", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("Jobs");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.Logs.BankAccountTransactionHistory", b => modelBuilder.Entity("ReallifeGamemode.Server.Entities.Logs.BankAccountTransactionHistory", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@@ -778,6 +790,8 @@ namespace ReallifeGamemode.Migrations
b.Property<int>("Handmoney"); b.Property<int>("Handmoney");
b.Property<int?>("JobId");
b.Property<int>("LogUserId"); b.Property<int>("LogUserId");
b.Property<string>("Name") b.Property<string>("Name")
@@ -813,6 +827,8 @@ namespace ReallifeGamemode.Migrations
b.HasIndex("GroupId"); b.HasIndex("GroupId");
b.HasIndex("JobId");
b.ToTable("Users"); b.ToTable("Users");
}); });
@@ -915,6 +931,17 @@ namespace ReallifeGamemode.Migrations
b.HasDiscriminator().HasValue("GroupVehicle"); b.HasDiscriminator().HasValue("GroupVehicle");
}); });
modelBuilder.Entity("ReallifeGamemode.Server.Entities.JobVehicle", b =>
{
b.HasBaseType("ReallifeGamemode.Server.Entities.ServerVehicle");
b.Property<int?>("JobId");
b.HasIndex("JobId");
b.HasDiscriminator().HasValue("JobVehicle");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.Saves.SavedVehicle", b => modelBuilder.Entity("ReallifeGamemode.Server.Entities.Saves.SavedVehicle", b =>
{ {
b.HasBaseType("ReallifeGamemode.Server.Entities.ServerVehicle"); b.HasBaseType("ReallifeGamemode.Server.Entities.ServerVehicle");
@@ -1052,6 +1079,10 @@ namespace ReallifeGamemode.Migrations
b.HasOne("ReallifeGamemode.Server.Entities.Group", "Group") b.HasOne("ReallifeGamemode.Server.Entities.Group", "Group")
.WithMany() .WithMany()
.HasForeignKey("GroupId"); .HasForeignKey("GroupId");
b.HasOne("ReallifeGamemode.Server.Entities.Job", "Job")
.WithMany()
.HasForeignKey("JobId");
}); });
modelBuilder.Entity("ReallifeGamemode.Server.Entities.UserBankAccount", b => modelBuilder.Entity("ReallifeGamemode.Server.Entities.UserBankAccount", b =>
@@ -1092,6 +1123,13 @@ namespace ReallifeGamemode.Migrations
.HasForeignKey("GroupId"); .HasForeignKey("GroupId");
}); });
modelBuilder.Entity("ReallifeGamemode.Server.Entities.JobVehicle", b =>
{
b.HasOne("ReallifeGamemode.Server.Entities.Job", "Job")
.WithMany()
.HasForeignKey("JobId");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.UserVehicle", b => modelBuilder.Entity("ReallifeGamemode.Server.Entities.UserVehicle", b =>
{ {
b.HasOne("ReallifeGamemode.Server.Entities.User", "User") b.HasOne("ReallifeGamemode.Server.Entities.User", "User")

View File

@@ -12,8 +12,9 @@ namespace ReallifeGamemode.Server.Models
{ {
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
base.OnConfiguring(optionsBuilder); optionsBuilder
optionsBuilder.UseMySql("Host=localhost;Port=3306;Database=gtav-devdb;Username=gtav-dev;Password=Test123"); .UseMySql("Host=localhost;Port=3306;Database=gtav-devdb;Username=gtav-dev;Password=Test123")
.UseLazyLoadingProxies();
} }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -99,5 +100,9 @@ namespace ReallifeGamemode.Server.Models
public DbSet<Entities.Group> Groups { get; set; } public DbSet<Entities.Group> Groups { get; set; }
public DbSet<Entities.GroupBankAccount> GroupBankAccounts { get; set; } public DbSet<Entities.GroupBankAccount> GroupBankAccounts { get; set; }
public DbSet<Entities.GroupVehicle> GroupVehicles { get; set; } public DbSet<Entities.GroupVehicle> GroupVehicles { get; set; }
// Jobs
public DbSet<Entities.Job> Jobs { get; set; }
public DbSet<Entities.JobVehicle> JobVehicles { get; set; }
} }
} }

View File

@@ -15,12 +15,13 @@
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0"> <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(ConfigurationName)' != 'ServerBuild'"> <ItemGroup Condition="'$(ConfigurationName)' != 'ServerBuild'">