C
C#2y ago
barcode

❔ EFCore migrations

Hello, I'm having issues applying configurations with ApplyConfigurationsFromAssembly and have to manually apply configurations // automatic
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
//manual
new CategoryConfiguration().Configure(modelBuilder.Entity<Category>());
...
new CenterConfiguration().Configure(modelBuilder.Entity<Center>());
new CategoryConfiguration().Configure(modelBuilder.Entity<Category>());
...
new CenterConfiguration().Configure(modelBuilder.Entity<Center>());
This occurs due to having a CenterCategory many to many table that I handle inside center configuration
2 Replies
barcode
barcodeOP2y ago
builder.HasMany(x => x.Categories)
.WithMany(x => x.Centers)
.UsingEntity<CenterCategory>(x =>
{
x.Property(y => y.CenterId)
.HasColumnType("int unsigned")
.HasColumnName("center_id")
.IsRequired();

x.Property(y => y.CategoryId)
.HasColumnType("int unsigned")
.HasColumnName("category_id")
.IsRequired();

x.HasKey(y=> new {y.CenterId, y.CategoryId});

x.HasIndex(y => y.CenterId);
x.HasIndex(y => y.CategoryId);

x.ToTable("centers_categories");

x.HasOne(y => y.Center)
.WithMany()
.HasForeignKey(y => y.CenterId);

x.HasOne(y => y.Category)
.WithMany()
.HasForeignKey(y => y.CategoryId);

x.Navigation(y => y.Center);
x.Navigation(y => y.Category);
});
builder.HasMany(x => x.Categories)
.WithMany(x => x.Centers)
.UsingEntity<CenterCategory>(x =>
{
x.Property(y => y.CenterId)
.HasColumnType("int unsigned")
.HasColumnName("center_id")
.IsRequired();

x.Property(y => y.CategoryId)
.HasColumnType("int unsigned")
.HasColumnName("category_id")
.IsRequired();

x.HasKey(y=> new {y.CenterId, y.CategoryId});

x.HasIndex(y => y.CenterId);
x.HasIndex(y => y.CategoryId);

x.ToTable("centers_categories");

x.HasOne(y => y.Center)
.WithMany()
.HasForeignKey(y => y.CenterId);

x.HasOne(y => y.Category)
.WithMany()
.HasForeignKey(y => y.CategoryId);

x.Navigation(y => y.Center);
x.Navigation(y => y.Category);
});
I believe when I do it automatically first it runs center and then category leading it to not find primary keys, error that I get:
The entity type 'CenterCategory' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fw
The entity type 'CenterCategory' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fw
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?