FoxySnake
FoxySnake
CC#
Created by FoxySnake on 1/14/2024 in #help
Foreign Key between Models with ef
Hi, I have an API in .net and I'm trying to link the property PackageTebexId from the model UserPackage to the property TebexId from the model Package but I can't achieve to do it. It always bind PackageTebexId to the Id of the model Package. Here's my models : User.cs
public class User : EntityBase
{
public List<UserPackage> Packages { get; set; } = new List<UserPackage>();
}
public class User : EntityBase
{
public List<UserPackage> Packages { get; set; } = new List<UserPackage>();
}
UserPackage.cs
public class UserPackage : EntityBase
{
public int UserId { get; set; }

public User User { get; set; }

public long PackageTebexId { get; set; }

public Package Package { get; set; }

public int Quantity { get; set; }
}
public class UserPackage : EntityBase
{
public int UserId { get; set; }

public User User { get; set; }

public long PackageTebexId { get; set; }

public Package Package { get; set; }

public int Quantity { get; set; }
}
Package.cs
public class Package : EntityBase
{
public long TebexId { get; set; }

public string Name { get; set; }

public string? ImageUrl { get; set; }

public decimal TotalPrice { get; set; }

public DateTime? ExpirationDate { get; set; }

public DateTime? CreationDate { get; set; }
}
public class Package : EntityBase
{
public long TebexId { get; set; }

public string Name { get; set; }

public string? ImageUrl { get; set; }

public decimal TotalPrice { get; set; }

public DateTime? ExpirationDate { get; set; }

public DateTime? CreationDate { get; set; }
}
EntityBase.cs
public abstract class EntityBase
{
public int Id { get; set; }
}
public abstract class EntityBase
{
public int Id { get; set; }
}
And here's my DbContext NewSkyDbContext.cs
public class NewSkyDbContext : DbContext
{
public NewSkyDbContext(DbContextOptions<NewSkyDbContext> dbContextOptions) : base(dbContextOptions)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

var modelTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !type.IsAbstract && !type.IsGenericType && type.IsClass && typeof(EntityBase).IsAssignableFrom(type));

foreach (var modelType in modelTypes)
{
modelBuilder.Entity(modelType);
}

// UserPackage
modelBuilder.Entity<UserPackage>()
.HasKey(x => new { x.PackageTebexId, x.UserId });

modelBuilder.Entity<UserPackage>()
.HasOne(x => x.User)
.WithMany(x => x.Packages)
.HasForeignKey(x => x.PackageTebexId);

modelBuilder.Entity<UserPackage>()
.HasOne(x => x.Package)
.WithMany()
.HasForeignKey(x => x.PackageTebexId);

// Package
modelBuilder.Entity<Package>()
.Property(x => x.TotalPrice)
.HasPrecision(12,2);

modelBuilder.Entity<Package>()
.HasIndex(x => x.TebexId)
.IsUnique();

SeedData(modelBuilder);
}

private void SeedData(ModelBuilder modelBuilder)
{

}
}
public class NewSkyDbContext : DbContext
{
public NewSkyDbContext(DbContextOptions<NewSkyDbContext> dbContextOptions) : base(dbContextOptions)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

var modelTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !type.IsAbstract && !type.IsGenericType && type.IsClass && typeof(EntityBase).IsAssignableFrom(type));

foreach (var modelType in modelTypes)
{
modelBuilder.Entity(modelType);
}

// UserPackage
modelBuilder.Entity<UserPackage>()
.HasKey(x => new { x.PackageTebexId, x.UserId });

modelBuilder.Entity<UserPackage>()
.HasOne(x => x.User)
.WithMany(x => x.Packages)
.HasForeignKey(x => x.PackageTebexId);

modelBuilder.Entity<UserPackage>()
.HasOne(x => x.Package)
.WithMany()
.HasForeignKey(x => x.PackageTebexId);

// Package
modelBuilder.Entity<Package>()
.Property(x => x.TotalPrice)
.HasPrecision(12,2);

modelBuilder.Entity<Package>()
.HasIndex(x => x.TebexId)
.IsUnique();

SeedData(modelBuilder);
}

private void SeedData(ModelBuilder modelBuilder)
{

}
}
16 replies
CC#
Created by FoxySnake on 10/2/2023 in #help
❔ Can't get Cookies Response from .net api
No description
11 replies