我正在尝试定义类别和项目之间的一对多关系(一个类别可以有一个或多个项目,一个项目可以有一个或没有类别)
public class Project : Entity {
public virtual string Title { get; set; }
public virtual Guid? CategoryId { get; set; }
public virtual Category Category { get; set; }
}
public class Category : Entity {
public virtual string Name { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
我已经定义了以下映射:
modelBuilder.Entity<Project>()
.MapSingleType(p => new {
ProjectId = p.Id,
p.CategoryId,
p.Title,
p.Slug,
p.ShortDescription,
p.Description,
p.CreatedOn,
p.UpdatedOn
})
.ToTable("Projects");
modelBuilder.Entity<Category>()
.MapSingleType(c => new {
CategoryId = c.Id,
c.Name,
c.CreatedOn,
c.UpdatedOn
})
.ToTable("Categories");
// relationships
modelBuilder.Entity<Project>()
.HasOptional<Category>(p => p.Category)
.WithMany()
.HasConstraint((p, c) => p.CategoryId == c.Id);
现在,虽然这似乎工作正常,但 EF 仍在生成 Categories_Products 表(用于多对多关联)。
我已禁用默认数据库初始化程序,但仍在生成此表。我究竟做错了什么?
谢谢本