My project has been created with a Clean Architecture and I get this error when I want to Insert a record to my table Users:

Screenshot of my error

My entity class User in the domain layer:

using Movie_asp.ValueObjects;

namespace Movie_asp.Entities;

public class User
{
    public UserId Id { get; private set; }
    public UserFullName FullName { get; private set; }
    public Username Username { get; private set; }
    public Password Password { get; private set; }
    public Email Email { get; private set; }
    public CreatedAt CreatedAt { get; private set; }

    public User()
    {
    }

    public User(UserId id, UserFullName userFullName, Username username, Password password, Email email, CreatedAt createdAt)
    {
        Id = id;
        FullName = userFullName;
        Username = username;
        Password = password;
        Email = email;
        CreatedAt = createdAt;
    }
}

My value object class :

namespace Movie_asp.ValueObjects;

public record CreatedAt(DateTime Value);

My DbContext class

using Application.Data;
using Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Movie_asp.Entities;
using Movie_asp.ValueObjects;

namespace Infrastructure;

public class ApplicationDbContext : DbContext, IApplicationDbContext, IUnitOfWork
{ 
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=localhost\SQLEXPRESS;Database=Test;Trusted_Connection=True;TrustServerCertificate=True;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .HasKey(i => i.Id);

        modelBuilder.Entity<User>()
            .Property(c => c.Id)
            .HasConversion(
                UserId => UserId.Value,
                value => new UserId(value)
                );
    }

    public DbSet<User> Users { get; set; }
}

It seems that Entity Framework Core treats my value object (CreatedAt class) as an entity.

I would be grateful if you could help me solve this problem.

0

There are 0 best solutions below