I have two entities, Project
and Tag
, defined as below
// Project.cs
namespace ProjectResume.Data.Entities
{
public class Project
{
public Project()
{
this.Tags = new List<Tag>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MinLength(3)]
[MaxLength(128)]
public string Name { get; set; }
[RegularExpression("([a-z0-9\-])+")]
public string Slug { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
[Required]
[MinLength(3)]
[MaxLength(512)]
public string ShortDescription { get; set; }
public string DetailedDescription { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
}
// Tag.cs
namespace ProjectResume.Data.Entities
{
public class Tag
{
public Tag()
{
this.Projects = new List<Project>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MinLength(3)]
[MaxLength(32)]
public string Name { get; set; }
[RegularExpression("([a-z0-9\-])+")]
public string Slug { get; set; }
[RegularExpression("#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})")]
public string Color { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
}
// DefaultDbContext.cs
namespace ProjectResume.Data
{
public class DefaultDbContext : DbContext
{
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Project>()
.HasMany(p => p.Tags)
.WithMany(t => t.Projects);
}
}
}
My goal is to create a 3rd entity, ProjectTagHighlight
, which should store one or more highlights per project per tag. I'd assume the table needs at least an Id
, ProjectId
, TagId
, Description
, but I'm unsure on how to set up this relation given the code I already have, given the fact that I don't specify a ProjectTag
entity myself.
The goal is to be able to access data in two different ways, either
Tag -> Project[] -> ProjectTagHighlight[]
or Project -> Tag[] -> ProjectTagHighlight[]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…