1

I have an Entity Framework model with a list member:

public class MyModel
{
        public int Id { get; set; }
        public string Name { get; set; }
        public int Points { get; set; }
        public List<Feature> Features { get; set; }
}

Where Feature is an enum.

I have a Seed() method in my configuration with an object literal:

new MyModel
{
    Id = 1,
    Name = "Test Name",
    Points = 2,
    Features = ???
}

How can I initialize Features as a literal list?

Geoff James
  • 3,122
  • 1
  • 17
  • 36
user3728304
  • 17
  • 1
  • 4
  • 1
    Seeing the answers (so far) nobody seems to notice that you can't store a `List` in the database. Think of it, which data type should the target database field have? `Feature` must be a class, mapped to its own table. – Gert Arnold Jun 25 '16 at 22:58
  • Possible duplicate: http://stackoverflow.com/q/32072732/861716 – Gert Arnold Jun 25 '16 at 23:07
  • Thanks @GertArnold - complete amateur hour for me, forgetting about `enum`s with Entity Framework. I've updated my answer below, which should now solve OP's question :) – Geoff James Jun 25 '16 at 23:38

3 Answers3

1

How can I initialize Features as a literal list?

UPDATE
As quite rightly pointed out by @Gert, you cannot use enums directly in an EF database (Oh, drats).

However, an enum is represented by an int, so the value can still be stored in the database, by the use of a class.

You will need to create a class in order to store the values in the database, and then you are able to refer to it from the MyModel class.

Like this:

public enum TypesOfFeature // The enum of features
{
    One = 1,
    Two = 2,
    Three = 3
    ... // etc.
}

public class Feature // The class to map to a table in your DB
{
    public int Id { get; set; }
    public TypesOfFeature Type { get; set; }
}

Add a DbSet<Feature> to your code-first model, so you can access this from your DB. Something like public DbSet<Feature> Features { get; set; } will do.

It might be a good idea to manually add the values into the database so you know their IDs will match the enum's int value. *You could do this using AddOrUpdate in theSeed() method - then when you need to add more, you could add them beneath*

You would need to return the Features you want, from the DB in order to then assign them to your MyModel.Features property.

Like so:

Feature featureOne = YourDbContext.Features.Single(x => x.Type == TypesOfFeature.One);
Feature featureTwo = YourDbContext.Features.Single(x => x.Type == TypesOfFeature.Two);
Feature featureThree = YourDbContext.Features.Single(x => x.Type == TypesOfFeature.Three);

Then, in the code, where you're initializing your MyModel object, you can then initialize the List<Feature>, passing in the required Feature objects you pulled out above:

var model = new MyModel
{
    Id = 1,
    Name = "Test Name",
    Points = 2,
    Features = new List<Feature>
               {
                   featureOne,
                   featureTwo,
                   featureThree
                }
}

Obviously, I don't know what values you have in your Feature enum, but above is just an example.

Hope this helps! :)

Geoff James
  • 3,122
  • 1
  • 17
  • 36
  • How can I use `Feature` in the `MyModel` seeds? I add `Feature` objects in the seed by using `context.Features.AddOrUpdate`. I then do `context.SaveChanges()` followed by `context.MyModel.AddOrUpdate` which I pass a `new MyModel { ..., Features = new List { context.Features.Single(x => x.Type == TypesOfFeature.One) } }`. The problem is that when I grab `MyModel` objects from `context` they have an empty `Features` list. – user3728304 Jun 26 '16 at 00:47
  • Where exactly is it you are trying to use `MyModel.Features` and get this problem? And what type of program are you writing (App, MVC etc.)? Are the `Features` empty or `null`? – Geoff James Jun 26 '16 at 01:03
  • I have a console application which I added the entity framework to. From my main method I create a context and get an object by doing `var firstMyModel = context.MyModel.First()` the problem is that `firstMyModel.Features` is `null` – user3728304 Jun 26 '16 at 01:11
  • Have you found this via the debugger or the output from the console window? Off the top of my head (I'm on mobile so a bit limited for space): You might want to declare your `Features` as a navigation property. A quick Google will show you this. I might suggest researching into Lazy Loading/Eager Loading in EF and how you can use it. You could also use Explicit Loading. As an example: where you do your query to select `MyModel` objects, use `context.MyModels.Include(x => x.Features)` where you grab them from the DB. You will need to include `using System.Data.Entity;` at the top of your code. – Geoff James Jun 26 '16 at 09:24
0

Initialize it as list of enum

 var model = new MyModel
        {
            Id = 1,
            Name = "Test Name",
            Points = 2,
            Features = new List<Feature>
            {
                Feature.Feature1,
                Feature.Feature2
            }
        };
Abdul Hadi
  • 1,229
  • 1
  • 11
  • 20
0
static void Main(string[] args)
    {
        MyModel mm = new MyModel();

        mm.Id = 1;
        mm.Name = "user3728304";
        mm.Points = 1;
        mm.Features = new List<Feature>{Feature.one,
                         Feature.two,
                         Feature.three};

        Console.Read();
    }
Saket Choubey
  • 916
  • 6
  • 11