-2

I'm fairly sure the answer to this is going to be that it can't be done but I'm going to ask anyway to make sure.

I am trying to define an ASP .Net MVC Model that contains a List property but EF seems to ignore it when creating the database schema.

Looking at this question, Entity Framework Code First List<string> Property Mapping , the solution seems to be to create a new class to use within the list. Obviously this seems like overkill for something as simple as a string so I was wondering if their is another way to do this with code first and Entity Framework.

Community
  • 1
  • 1
AverageMarcus
  • 903
  • 1
  • 9
  • 26

1 Answers1

0

You need to tell EF how to represent this in the database

What is the list of string representing? You need to remember you are defining how this data will be stored in a database.

These are the reasons you need to define the model as shown in @Ladislav Mrnka post

Update

To save confusion, I would do exactly as @Ladislav Mrnka suggests:

public class Container
{
    public int Id { get; set; }
    public string Name { get; set; }   

    public virtual ICollection<User> Users
    {
        get;
        set;
    }
}

public class User
{
    public int Id { get; set; }
    public string Text { get; set; }
}
Community
  • 1
  • 1
NinjaNye
  • 7,046
  • 1
  • 32
  • 46
  • The strings will be representing either a list of user ID from another service or a list of areas. – AverageMarcus Mar 06 '13 at 13:43
  • Presumably this needs to be stored in your database? Hence the reason for it being in you models. Therefore you need to define in the model how this is to be represented in your database – NinjaNye Mar 06 '13 at 13:45
  • Would you recommend a separate class (and therefore table) for each of these? It seems messy to use a single "TextOptions" to contain different data simply to get around a List. – AverageMarcus Mar 06 '13 at 13:59
  • Definately, an almost identical solution to the related post by ladislav Mrnka. This will allow ef to know how to map this to a schema. It also enables you to have proper relationships between these user id's later down the line – NinjaNye Mar 06 '13 at 14:04
  • Thanks. :) Cleared things up a lot. – AverageMarcus Mar 06 '13 at 14:07