Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

how to implement a many to many relationship #412


User avatar
0
cicciottino created

I'd like to set up a tagging system for an entity in order to get a many to many relationship How is it possible achieve this sort relationship in abp?

Using a tag entity, an article entity and between them...? should i create another entity like "artcicles_tags"? Must this middle entity be derived from "Entity<long>" as well? and the mapping configuration is by convention or i must mapping it manually? Could you give me some clues? some suggestion?


2 Answer(s)
  • User Avatar
    0
    ddnils created

    Have a look at Entity Framework.

    You can pretty much use either. If you just want Many to Many relationship, thats possible by just creating your Entities the right way:

    public class Article: Entity<long> {
      public  long Id {get;set;}
      ...
      public virtual ICollection<Tag> Tags {get;set;}
    }
    
    public class Tag: Entity<long> {
      ...
      public virtual ICollection<Article> Articles {get;set;}
    }
    

    This should work. (EF creates your tables)

    But if you want your relationship table to have additional fields (ie. CreationDate) you need to use FluentApi.

    See this document for help: <a class="postlink" href="http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx">http://www.entityframeworktutorial.net/ ... first.aspx</a>

  • User Avatar
    0
    cicciottino created

    oh thanks a lot i did it quickly and easily

    1. Added the navigation properties
    2. Add-Migration
    3. Update-Database

    and it's done, nice.