Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

entity framework - Use join-table of many-to-many relationship as source for another relation

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[]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because you have used the third-part ProjectTagHighlight. You can access data as

ProjectTagHighlight -> Tag , ProjectTagHighlight -> Project

They form a many-to-many relationship through ProjectTagHighlight.

        modelBuilder.Entity<ProjectTagHighlight>()
            .HasOne(x => x.project)
            .WithOne(y => y.projectTagHighlight)
            .HasForeignKey<ProjectTagHighlight>(f => f.ProjectId);
        modelBuilder.Entity<ProjectTagHighlight>()
            .HasOne(x => x.tag)
            .WithOne(y => y.projectTagHighlight)
            .HasForeignKey<ProjectTagHighlight>(f => f.TagId);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...