Entity Framework class with list of object

15,421

Solution 1

Right now, you are trying to store the entire list of "Features" into the "Project" database when all you need to store is a unique identifier for each "Feature" which is the Feature.id property. So "Project" could be:

public class project
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid id { get; set; }

    [Required]
    public string aliasName { get; set; }

    [ForeignKey("Features")]
    public virtual List<int> featureIds { get; set; }
}

When you need specific Feature data, just query the Feature database for the specific feature id. Here is more information about using foreign keys within Entity.

Solution 2

In Project class:

public virtual ICollection<feature> feature { get; set; }

And in dbContext:

modelBuilder.Entity<project>().HasMany(i => i.feature).WithMany();
Share:
15,421
user2633804
Author by

user2633804

Updated on June 11, 2022

Comments

  • user2633804
    user2633804 almost 2 years

    I'm using EF 6.0 and ASP.NET MVC with C# and I have two classes - project and feature:

    public class project
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid id { get; set; }
    
        [Required]
        public string aliasName { get; set; }
    
        public virtual List<feature> features { get; set; }
    }
    

    and

    public class feature
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int id { get; set; }
    
        [Required]
        public string name { get; set; }
    
        [DataType(DataType.MultilineText)]
        public string description { get; set; }
    }
    

    The end-user will pick more than one feature per project, and each project can contain different features.

    How can I setup this in EF and store this selection in db?

    In dbContext I have at this moment:

    public DbSet<project> Projects { get; set; }
    public DbSet<feature> Features { get; set; }
    
  • user2633804
    user2633804 over 7 years
    And how I can save this relationship list into db? Should I add something to my dbcontext?
  • Kazenzakis
    Kazenzakis over 7 years
    Are you creating a new db to store this or trying to update an old one. If you haven't made the database yet, you shouldn't need to add anything. Otherwise, you might need to migrate the database.
  • user2633804
    user2633804 over 7 years
    The db is a new one. When I build there is no table that links featuresIds with projectId, only Projects and Features tables.
  • Kazenzakis
    Kazenzakis over 7 years
    You should only have those two tables since those house all of the data you need. The featureids just hold a list of ids located in the Features table. For example if Project has two features, feature id will include the feature with id = 1 and id = 2. If you need to know what are the properties of those features, you just query the Feature table for Feature.id =1 and Feature.id = 2.
  • user2633804
    user2633804 over 7 years
    But Entity Framework should create a table with projectId and featureId columns no? If each project contains multiple features associate (different projects could share same features) I need to store this info somewhere. The features table is just a repository of template features.