C
C#2mo ago
ReAn

EF Core Defined in Library Assembly, Configured in ASPNET Asembly

Hey, I've been away from netcore for a while and im getting back into it. I have two assemblies right now: * Journey.Web * Journey.Application I haven't decided to seperate much yet, just keeping the entrypoint seperate from the core logic. Thus most behavior is in the Journey.Application assembly. I've started to set up some EF Core, I've created an ApplicationDbContext in Journey.Application and started building configurations for my entities using the IEntityTypeConfiguration<T> pattern with the following in my Application Context:
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options), IAccountsContext
{
public DbSet<Account> Accounts { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
}
}
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options), IAccountsContext
{
public DbSet<Account> Accounts { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
}
}
This is my configuration for Account:
public void Configure(EntityTypeBuilder<Account> a)
{
a.ToTable("accounts", "accounts");
a.HasKey(x => x.Id);
a.Property(x => x.Id)
.HasConversion(i => i.Id, id => new AccountId(id))
.HasValueGenerator(idGeneratorFactory.Create);
a.Property(x => x.Email)
.HasConversion(e => e.Value, e => new Email(e));
a.Property(x => x.Name)
.HasConversion(n => n.Value, n => new AccountName(n));
}
public void Configure(EntityTypeBuilder<Account> a)
{
a.ToTable("accounts", "accounts");
a.HasKey(x => x.Id);
a.Property(x => x.Id)
.HasConversion(i => i.Id, id => new AccountId(id))
.HasValueGenerator(idGeneratorFactory.Create);
a.Property(x => x.Email)
.HasConversion(e => e.Value, e => new Email(e));
a.Property(x => x.Name)
.HasConversion(n => n.Value, n => new AccountName(n));
}
Note that I have configured Account has a key x.Id and The context should be picking up this configuration via the assembly binding in OnModelCreating. However, when I run the following command from my solution:
dotnet ef migrations add AccountsSchemaAndTable --project Journey.Application --startup-project Journey.Web --context ApplicationDbContext
dotnet ef migrations add AccountsSchemaAndTable --project Journey.Application --startup-project Journey.Web --context ApplicationDbContext
I get the following error:
Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'The entity type 'Account' 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/fwlink/?linkid=2141943.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'The entity type 'Account' 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/fwlink/?linkid=2141943.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
It does not seem like it's picking up the EntityTypeConfiguration since it doesn't seem to pick up the .HasKey definition. I also tried putting an exception in the Type Configuration method and it did not fail with my exception, so im relatively certain it's not executing my model binders, does anyone know what I've done wrong here?
1 Reply
ReAn
ReAn2mo ago
I'm guessing it's because I have a dependency, and the instantiation here is not happening in the DI Context? Thus it's not created, and this model is not configured. It seems that way, after moving the deps to my context and instantiating the configuration in the OnModelCreating it works. Does anyone have a better way for allowing the EntityTypeConfiguration<T> to work from the Service Collection?
Want results from more Discord servers?
Add your server