简体   繁体   中英

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: 在此输入图像描述

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>();
    }
}


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. 在此输入图像描述

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; }


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.

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; }
}

具有多个相关年龄组,残疾和策略类型的文章的数据模型

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM