-1

I am new to .NET so excuse my lack of knowledge.

I'm trying to create a simple Web API using Fast Endpoints and Ulid as id to the entities and Entity Framework Core.

Here is my code:

PostEntity.cs:

using Api.Auth.Data;
using Api.Common;
using Vogen;

namespace Api.Posts.Data;

public class PostEntity : BaseEntity<PostEntityId>
{
    public string Caption { get; set; } = string.Empty;
    public required User User { get; set; }
}

[ValueObject<Ulid>]
public partial class PostEntityId
{
}

This is the base entity:

Api/Common/Entity.cs:

namespace Api.Common;

public class BaseEntity
{
    public DateTimeOffset? DeletedAt { get; set; }
}

public class BaseEntity<T> : BaseEntity
{
    public required T Id { get; set; }
}

I used partial class to divide the DbContext into multiple files

Api/Data/SMDbContext.cs:

using Api.Auth.Data;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Api.Data
{
    public partial class SMDbContext : IdentityDbContext<User>
    {
        public SMDbContext(DbContextOptions options) : base(options)
        {
        }
    }
}

And here the file where I put the DbSet of PostEntity - Api/Posts/Data/SMDbContext.cs:

using Api.Posts.Data;
using Microsoft.EntityFrameworkCore;

namespace Api.Data
{
    public partial class SMDbContext
    {
        public DbSet<PostEntity> Posts => Set<PostEntity>();        
    }
}

Now must of the code structure I just saw a project that do it this way, and I liked it, but really I don't know how must of this stuff work.

So when I try to make the migrations I get this error:

$ dotnet ef migrations add initial_migration
Build started...
Build succeeded.

Unable to create a 'DbContext' of type ''.
The exception 'The entity type 'PostEntity' 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

What should I do for this to work?

1
  • Hello have you tried the solution provided? Did you able to made any progress on this? Commented May 23, 2024 at 8:30

1 Answer 1

1

Unable to create a 'DbContext' of type ''. The exception 'The entity type 'PostEntity' 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

Based on your code snippet and the error message you got while executing migration command telling that, Entity Framework Core (EF Core) doesn't recognize a primary key for your PostEntity.

As you may know, when you defines class in entity framework that relies on a primary key to uniquely identify each entity in the database. So the keys are essential in order to map your database relationship or constrains.

To fix the issue, either you have to define the primary key within Your PostEntity class which inherits from BaseEntity<PostEntityId>, and BaseEntity defines an Id property of type T, which should be recognized as the primary key. This is related how EF Core interprets the Id property.

Alternatively, you also can define the relationship mapping with your OnModelCreating class as well.

However, since you're using a custom PostEntityId class with Ulid, so you could modify it to include the primary key property. See the example below:

[ValueObject<Ulid>]
public partial class PostEntityId
{
    public Ulid Value { get; }

    public PostEntityId(Ulid value)
    {
        Value = value;
    }
}

Note: Ensure you have the Ulid package installed. You can install it via NuGet Package Manager. In addition, please refer to this official document for how you could configure the key mapping.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.