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?