I'm trying to create a database via Entity Framework Core.
I have the following models in my 'Domain' project:
public abstract class Note
{
protected Note(string mainBody, DateTime publicationMoment, User author)
{
MainBody = mainBody;
PublicationMoment = publicationMoment;
AuthorId = author.Id;
Author = author;
}
public int Id { get; set; }
[Required]
[MaxLength(1500)]
public virtual string MainBody { get; set; }
[Required]
[NoteDateTime]
public DateTime PublicationMoment { get; set; }
[Required]
public int AuthorId { get; set; }
[Required]
public User Author { get; set; }
}
public class Comment : Note
{
public Comment(Post post, string mainBody, DateTime publicationMoment, User author) :
base(mainBody, publicationMoment, author)
{
Post = post;
PostId = post.Id;
}
[Required]
public int PostId { get; set; }
[Required]
public Post Post { get; set; }
}
public class Post : Note
{
public Post(string title, string mainBody, DateTime publicationMoment, User author) :
base(mainBody, publicationMoment, author) => Title = title;
[Required]
[MaxLength(100)]
public string Title { get; set; }
public IList<Comment> Comments { get; set; } = new List<Comment>();
}
public class User
{
public User(string nickname, int age, Gender gender)
{
Nickname = nickname;
Age = age;
Gender = gender;
}
public int Id { get; set; }
[Required]
[MaxLength(20)]
public string Nickname { get; set; }
[Required]
[Range(14, 130)]
public int Age { get; set; }
[Required]
public Gender Gender { get; set; }
}
And the BlogContext class inside my 'Infrastructure' project:
public class BlogContext : DbContext
{
public DbSet<User> Users => Set<User>();
public DbSet<Post> Posts => Set<Post>();
public DbSet<Comment> Comments => Set<Comment>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var config = builder.Build();
optionsBuilder.UseSqlServer(config.GetConnectionString("Default"));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasAlternateKey(u => u.Nickname);
}
}
When I'm trying to add my first migration in Package Manager Console (like so: Add-Migration BlogDbCreation, I'm getting the following error:
"No suitable constructor was found for entity type 'Comment'. The following constructors had parameters that could not be bound to properties of the entity type: Cannot bind 'post', 'author' in 'Comment(Post post, string mainBody, DateTime publicationMoment, User author)' Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound."
So, how can I solve it?
Pass the id:s in the constructors instead of the reference types and let EF do the mapping for you.
Have you considered dropping the 'Note' inheritance in favor of a strictly one-to-many relationship between Post and Comment?
public abstract class Entity
{
public Guid Id { get; set; }
public DateTimeOffset Created { get; set; }
}
public class Comment : Entity
{
private Comment() { }
public Comment(string mainBody, DateTime publicationMoment, Guid authorId)
{
MainBody = mainBody;
PublicationMoment = publicationMoment;
AuthorId = authorId;
}
public Post Post { get; set; } = null!;
public string MainBody { get; set; }
public DateTime PublicationMoment { get; set; }
public Guid AuthorId { get; set; }
public User Author { get; set; } = null!;
}
public class Post : Entity
{
private Post() { }
public Post(string title, string mainBody, DateTime publicationMoment, Guid authorId)
{
Title = title;
MainBody = mainBody;
PublicationMoment = publicationMoment;
AuthorId = authorId;
}
public string Title { get; set; }
public string MainBody { get; set; }
public DateTime PublicationMoment { get; set; }
public Guid AuthorId { get; set; }
public User Author { get; set; } = null!;
public IList<Comment> Comments { get; set; } = new List<Comment>();
}
public class User : Entity
{
private User() { }
public User(string nickname, int age)
{
Nickname = nickname;
Age = age;
}
public string Nickname { get; set; } = null!;
public int Age { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With