FoxySnake
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
UserPackage.cs
Package.cs
EntityBase.cs
And here's my DbContext
NewSkyDbContext.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>();
}
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; }
}
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; }
}
public abstract class EntityBase
{
public int Id { get; set; }
}
public abstract class EntityBase
{
public int Id { get; set; }
}
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