Question

I have incorporated a couple of enums in a class IMage. I am using code first and am attempting to seed the Images table. Unfortunately no matter what enum valkue I seed with the table is populated with a value of 0. I am targetting .net 4.5 and using ef 6.Below is an example of my code.

public class Image: Entity
    {
        public string Name { get; set; }

        public string Url { get; set; }

        [Required]
        public ImageSize Size { get; set; }

        [Required]
        public ImageType Type { get; set; }
    }

    public enum ImageSize
    {
        Large = 1,
        Medium,
        Small,
        Icon
    }

    public enum ImageType
    {
        Profile = 1,
        Inventory,
        Calendar
    }

Seed Method:

//New Inventory Item
            context.InvItems.AddOrUpdate(
                b => b.Name,
                new InvItem
                {
                    Id = 1,
                    Name = "Custom Bracelet",
                    Description = "Beautiful Steel Casing",
                    InventoryImages = new List<Image>{
                        new Image{
                            Name = "bracelet_1.jpg",
                            Url = "",
                            Type = ImageType.Calendar,
                            Size = ImageSize.Medium,
                            CreateDate = DateTime.Now,
                            ModifiedDate = DateTime.Now
                        }                                
                    },
                    Price = 20.00,
                    CreateDate = DateTime.Now,
                    ModifiedDate = DateTime.Now
                }
            );

DB Outcome:

Id  Name             Url    Size    Type    
1   bracelet_1.jpg           0       0

As you can see the enum value for size should be 2 and the enum value for type should be 3. Any help would be greatly appreciated as I am new to incorporating enums into my ef development work.

Thanks in advance,

Was it helpful?

Solution

Thank you all for your quick responses. Actually there was not a problem with the enums. For some reason ef decided to update the database on my .\sqlserverexpress instance as opposed to my localdb instance. After reading through the update-database -verbose message I noticed it had decided to switch sqlserver instances on me and so the changes were not reflected by the enum assignment as shown above.

Now everything is running splendidly besides having to check a different instance.

Id  Name            Url   Size    Type
1   bracelet_1.jpg          2      3

I am now looking into how to explicitly set what sqlserver instance ef will write to. Thank you again for your advise and I'm sure it all would have worked!!

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top