© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
2 replies
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
Journey.Application
assembly.

I've started to set up some EF Core, I've created an
ApplicationDbContext
ApplicationDbContext
in
Journey.Application
Journey.Application
and started building configurations for my entities using the
IEntityTypeConfiguration<T>
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
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
Account
has a key
x.Id
x.Id
and The context should be picking up this configuration via the assembly binding in
OnModelCreating
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
EntityTypeConfiguration
since it doesn't seem to pick up the
.HasKey
.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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

EF Core
C#CC# / help
2y ago
❔ Mixing EF Core and another SQL library
C#CC# / help
3y ago
✅ aspnet core not recognizing images
C#CC# / help
3y ago
❔ Polymorphic relationships in EF Core / .NET Core
C#CC# / help
3y ago