Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:43:16+00:00 2026-05-22T00:43:16+00:00

I have following Classes and interfaces.. public interface ITaggable { ICollection<Tag> Tags { get;

  • 0

I have following Classes and interfaces..

 public interface ITaggable
    {
         ICollection<Tag> Tags { get; set; }
    }

 public class Book :ITaggable
    {
        [DatabaseGenerated(DatabaseGenerationOption.Identity)] 
        public Guid BookId { get;set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public virtual ICollection<Tag> Tags {get; set;}
    }

public class Pen:ITaggable
    {
        [DatabaseGenerated(DatabaseGenerationOption.Identity)]
        public Guid PenId { get; set; }
        public string Color { get; set; }
        public virtual ICollection<Tag> Tags {get; set;}
    }

public class Tag
    {
        [DatabaseGenerated(DatabaseGenerationOption.Identity)] 
        public Guid TagId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<ITaggable> Items { get; set; }
    }

For the above model it generates the following table structure

Book –>
BookId , Title , Author

Pen –>
PenId , Color

Tag –>
TagId , Name ,BookBookId , PenPenId

And when i insert the following data

            Tag tag = new Tag();
            tag.Name = "Stationary";

            Book b1 = new Book();
            b1.Title = "Head first c#";
            b1.Author = "Betty";
            b1.Tags = new List<Tag>() { tag };

            Book b2 = new Book();
            b2.Title = "Head first Java";
            b2.Author = "Katty";
            b2.Tags = new List<Tag>() { tag };

            Pen p = new Pen();
            p.Color = "Red";
            p.Tags = new List<Tag>() { tag };

            context.Books.Add(b1);
            context.Books.Add(b2);
            context.Pens.Add(p);
            context.SaveChanges();

It does not insert the Tag data for the second book

What i want to accomplish is that i want to implement a three table tagging system
shown here adhering to my class structure

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:43:16+00:00Added an answer on May 22, 2026 at 12:43 am

    Yes it will not store the tag info for one of the books because your tag instance can be associated only with single book. That would require your many-to-many relation between Book and Tag but your relation is one-to-many. Relation between Pen and Tag is also one-to-many. That is clearly visible by foreign keys in Tag table.

    The problem is that ICollection<ITaggable> Items is skipped by EF code first – code first doesn’t work with interfaces. You must define your Tag as:

    public class Tag
    {
        [DatabaseGenerated(DatabaseGenerationOption.Identity)] 
        public Guid TagId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Book> Books { get; set; }
        public virtual ICollection<Pen> Pens { get; set; }
    }
    

    This will map many-to-many between Book and Tag and many-to-many between Pen and Tag. If you want also collection of ITaggable you can expose another property concatenating Books and Pens.

    If you really want one-to-many relation then you cannot expect that tag will be associated with multiple books and in such case your Tag entity should look like:

    public class Tag
    {
        [DatabaseGenerated(DatabaseGenerationOption.Identity)] 
        public Guid TagId { get; set; }
        public string Name { get; set; }
        public virtual Book Book { get; set; }
        public virtual Pen Pen { get; set; }
    }
    

    Again you can create Items as computed property. Any combination of relation should be obvious from these examples.

    Edit:

    If you don’t want to expose navigation properties in Tag you must use fluent-api:

    public class Context : DbContext
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Pen> Pens { get; set; }
        public DbSet<Tag> Tags { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Book>()
                        .HasMany(b => b.Tags)
                        .WithMany();
    
            modelBuilder.Entity<Pen>()
                        .HasMany(p => p.Tags)
                        .WithMany();
        }
    }
    

    Further fluent api reference can be found on ADO.NET team blog but it is not up to data (it is for CTP5).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Interfaces: public interface ITemplateItem { int Id { get; set;
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: public class Person { public String FirstName { set;
I have the following classes defined: public interface Thingy { ... } public class
I have following classes. class A { public: void fun(); } class B: public
I have two interfaces like these: public interface IMyInterface1 { string prop1 { get;
Imagine we have following classes: public interface MyInterface<T> { List<T> getList(T t); } abstract
I have something like following: public interface A { .... } public abstract class
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have the following 3 classes Book Product SpecialOptions There are many Books, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.