我正在尝试通过 Entity Framework Core 创建数据库。
我的“域”项目中有以下模型:
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; }
}
我的“基础设施”项目中的 BlogContext 类:
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);
}
}
当我尝试在包管理器控制台中添加我的第一个迁移时(像这样:
Add-Migration BlogDbCreation
,我收到以下错误:
“找不到适合实体类型‘Comment’的构造函数。以下构造函数的参数无法绑定到实体类型的属性: 无法在“评论(发布帖子、字符串主体、DateTime publicationMoment、用户作者)”中绑定“帖子”、“作者” 请注意,只有映射属性可以绑定到构造函数参数。无法绑定到相关实体的导航,包括对拥有类型的引用。”
那么,我该如何解决呢?
在构造函数中传递 id:s 而不是引用类型,让 EF 为您进行映射。
您是否考虑过放弃“Note”继承以支持 Post 和 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; }
}