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