C
C#2y ago
demndev

✅ can't map an entity to DB

i'm getting an error on adding a mapped domain entity
3 Replies
demndev
demndevOP2y ago
I have a NoteEntity:
public class NoteEntity
{
public int Id { get; set; }
public Title Title { get; }
public Text Text { get; }
public DateTime CreationDateTime { get; }
public DateTime UpdateDateTime { get; }

public UserEntity Author { get; private set; }
public int AuthorId { get; private set; }

public NoteEntity(Title title, Text text, DateTime creationDateTime, DateTime updateDateTime, UserEntity author)
{
Title = title;
Text = text;
CreationDateTime = creationDateTime;
UpdateDateTime = updateDateTime;
AuthorId = author;
}
}
public class NoteEntity
{
public int Id { get; set; }
public Title Title { get; }
public Text Text { get; }
public DateTime CreationDateTime { get; }
public DateTime UpdateDateTime { get; }

public UserEntity Author { get; private set; }
public int AuthorId { get; private set; }

public NoteEntity(Title title, Text text, DateTime creationDateTime, DateTime updateDateTime, UserEntity author)
{
Title = title;
Text = text;
CreationDateTime = creationDateTime;
UpdateDateTime = updateDateTime;
AuthorId = author;
}
}
and a UserEntity:
public class UserEntity
{
public int Id { get; set; }
public Username Username { get; }
public Password Password { get; } // TODO: work with password hashing
private readonly List<NoteEntity> _notes = new();
public IEnumerable<NoteEntity> Notes => _notes.AsReadOnly();

public UserEntity(string password, string username)
{
Password = new Password(password);
Username = new Username(username);
}
}
public class UserEntity
{
public int Id { get; set; }
public Username Username { get; }
public Password Password { get; } // TODO: work with password hashing
private readonly List<NoteEntity> _notes = new();
public IEnumerable<NoteEntity> Notes => _notes.AsReadOnly();

public UserEntity(string password, string username)
{
Password = new Password(password);
Username = new Username(username);
}
}
here's a NoteConfiguration:
public class NoteConfiguration : IEntityTypeConfiguration<NoteEntity>
{
public void Configure(EntityTypeBuilder<NoteEntity> builder)
{
builder.HasKey(n => n.Id);

builder.Property(n => n.Title)
.HasConversion(
title => title.Value,
value => new Title(value));

builder.Property(n => n.Text)
.HasConversion(
text => text.Value,
value => new Text(value));

builder.Property(n => n.CreationDateTime)
.HasConversion(
dt => dt.ToString(CultureInfo.InvariantCulture),
value => DateTime.Parse(value));

builder.Property(n => n.UpdateDateTime)
.HasConversion(
dt => dt.ToString(CultureInfo.InvariantCulture),
value => DateTime.Parse(value));

builder
.HasOne<UserEntity>("Author")
.WithMany()
.HasForeignKey(n => n.AuthorId)
.IsRequired();
}
}
public class NoteConfiguration : IEntityTypeConfiguration<NoteEntity>
{
public void Configure(EntityTypeBuilder<NoteEntity> builder)
{
builder.HasKey(n => n.Id);

builder.Property(n => n.Title)
.HasConversion(
title => title.Value,
value => new Title(value));

builder.Property(n => n.Text)
.HasConversion(
text => text.Value,
value => new Text(value));

builder.Property(n => n.CreationDateTime)
.HasConversion(
dt => dt.ToString(CultureInfo.InvariantCulture),
value => DateTime.Parse(value));

builder.Property(n => n.UpdateDateTime)
.HasConversion(
dt => dt.ToString(CultureInfo.InvariantCulture),
value => DateTime.Parse(value));

builder
.HasOne<UserEntity>("Author")
.WithMany()
.HasForeignKey(n => n.AuthorId)
.IsRequired();
}
}
and a UserConfiguration:
public class UserConfiguration : IEntityTypeConfiguration<UserEntity>
{
public void Configure(EntityTypeBuilder<UserEntity> builder)
{
builder.HasKey(u => u.Id);

builder.Property(u => u.Username)
.HasConversion(
u => u.Value,
value => new Username(value));

builder.Property(u => u.Password)
.HasConversion(
p => p.Value,
value => new Password(value));

// builder // i tried with and without it; but it still doesn't work
// .HasMany(u => u.Notes)
// .WithOne(n => n.Author)
// .HasForeignKey("AuthorId")
// .IsRequired();
}
}
public class UserConfiguration : IEntityTypeConfiguration<UserEntity>
{
public void Configure(EntityTypeBuilder<UserEntity> builder)
{
builder.HasKey(u => u.Id);

builder.Property(u => u.Username)
.HasConversion(
u => u.Value,
value => new Username(value));

builder.Property(u => u.Password)
.HasConversion(
p => p.Value,
value => new Password(value));

// builder // i tried with and without it; but it still doesn't work
// .HasMany(u => u.Notes)
// .WithOne(n => n.Author)
// .HasForeignKey("AuthorId")
// .IsRequired();
}
}
when I try to add a NoteEntity, I get an error:
No suitable constructor was found for entity type 'NoteEntity'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'author' in 'NoteEntity(Title title, Text text, DateTime creationDateTime, DateTime updateDateTime, UserEntity author)'
No suitable constructor was found for entity type 'NoteEntity'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'author' in 'NoteEntity(Title title, Text text, DateTime creationDateTime, DateTime updateDateTime, UserEntity author)'
JakenVeina
JakenVeina2y ago
yes, Author is a navigation property of NoteEntity, not a data property, ergo it is not bindable, ergo there's no bindable property for the author parameter to bind to A) navigation properties are not loaded at the time that their source entities are loaded B) navigation properties may not be loaded at all it wouldn't make sense to set them in the constructor, and it ESPECIALLY wouldn't make sense for them to be non-nullable in that constructor
demndev
demndevOP2y ago
thank you!
Want results from more Discord servers?
Add your server