1011 1110
1011 1110

Reputation: 781

Handling a 'Many to One' Relationship in MVC 4 ASP.NET

I have a database I created from my models and it is quite similar to the image below: enter image description here

However, making it actually work with CRUD operations is a bit different :(

So I took my approach on how I handled 'normal' tables and tried to make it work, but I couldn't.

here's my DAL:

public class ArticleEntities : DbContext
{
    public DbSet<AgeGroup> AgeGroups { get; set; }
    public DbSet<Disabilities> Disabilitiess { get; set; }
    public DbSet<StrategyType> StrategyTypes { get; set; }
    public DbSet<Article> Articles { get; set; }
    public DbSet<UserProfile> Profiles { get; set; }
    public DbSet<DataToArticle> DataToArticles { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
}

What I want to do

From the website point of view, I need to access the create page (view) of the Article table and have drop down lists for AgeGroup, Disability, and StrategyType. With also having the ability to add more drop down lists on click (of a plus sign for example) so each category(table-Age Group, Disability, StrategyType) can have more than one value assigned to it.enter image description here

My models for Article, AgeGroup, Disability, StrategyType are done correctly I believe.I added this line:

public virtual List<DataToArticle> AgeGroupToArticle { get; set; }

To all of them to make them connect to the DataToArticle table. I also added this code to the DataToArticle table:

    public virtual AgeGroup AgeGroup { get; set; }

    public virtual Disabilities Disabilities { get; set; }

    public virtual StrategyType StrategyType { get; set; }

What I think needs to be done, but don't know how

In the ArticleController I know I have to add all the additional information from the other tables into it. I have attempted to use Linq, but no avail, but I'm pretty sure it can be done this way.

Also in the Article create view, I have to be able to access the information that has to be displayed in the drop down lists from the other tables.

I could do all of this if I didn't have the many to one relationship table lol, it really changes everything for me.

Upvotes: 4

Views: 4142

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

I think part of what is throwing you off is that you're putting all the relations into a single row in a single table. There are a couple of different ways that you can handle it depending on whether the related types really need to be entities or are simply values. If there are simply values, then you could create a secondary table to old each type of value. In this table you'd have the article id as a foreign key and the value associated with it. That is how I would do a one-to-many relationship.

If the associated values really are entities in and of themselves (In my opinion it would consist of more than just an id and a name if they were), then you'd have a join table per entity type with the key consisting of the article id and the entity id. What you'd really be modeling is a many-to-many relationship between the articles and each entity type.

I'll assume the first case is what you really want. In your article, then, you'd have properties for age groups, disabilities, and strategy types.

public ICollection<AgeGroup> AgeGroups { get; set; }
public ICollection<Disability> Disabilities { get; set; }
public ICollection<StrategyType> StrategyTypes { get; set; }

And in each entity you'd have an id, article id, and the value, plus a reference to the related article (through the article id), for example:

public class AgeGroup
{
     public int Id { get; set; }
     public int ArticleId { get; set; }
     public string AgeName { get; set; }

     public Article Article { get; set; }
}

Data model for article with multiple related age groups, disabilities, and strategy types

Upvotes: 3

jarnail
jarnail

Reputation: 414

If you are using EF Code First and just looking for a basic example to get started with One to Many relationship implementation in MVC, there are many examples available online. Here is one of them on asp.net/mvc.

Upvotes: 0

Related Questions