Hi,
I am developing a small blog application in ASP.NET Core 6. I am using Identity on ASP.NET Core.
I'm planning to use the CRUD Scaffolding to create the razor pages. A BlogPost must only be created by authenticated users. And each BlogPost should always be immediately linked to the authenticated user.
I created a model BlogPost and added the fields Author, Title, Content and CreationDate. No field is nullable and the constructor requires to pass a user object, which should be the currently signed in user later in the pages. When I run Add-Migration, I get this error:
No suitable constructor was found for entity type 'BlogPost'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'author' in 'BlogPost(IdentityUser author, string title, string content)'.
What do I need to correct to get this working like I intend to?
BlogPost.cs
csharp
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
namespace BlogPost.Models
{
public class BlogPost
{
public string Id { get; set; }
[Required]
public IdentityUser Author { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Content { get; set; }
[Required]
public DateTime CreationDate { get; set; }
public BlogPost(IdentityUser author, string title, string content)
{
Author = author;
Title = title;
Content = content;
CreationDate = DateTime.UtcNow;
}
}
}
