none
Entity Framework how to save a List of type int to database RRS feed

  • Question

  • I am trying to insert data from a Json string into a SQL database using Entity Framework. I am generating the models from Json2CSharp which gives the following classes.

    public class Dna
    {
        public string dnaType { get; set; }
        public int dnaTypeHash { get; set; }
        public string packedDna { get; set; }
    }
    
    public class CharacterColor
    {
        public string name { get; set; }
        public List<int> colors { get; set; }
    }
    
    public class WardrobeSet
    {
        public string slot { get; set; }
        public string recipe { get; set; }
    }
    
    public class RootObject
    {
        public string packedRecipeType { get; set; }
        public string name { get; set; }
        public string race { get; set; }
        public List<Dna> dna { get; set; }
        public List<CharacterColor> characterColors { get; set; }
        public List<WardrobeSet> wardrobeSet { get; set; }
        public string raceAnimatorController { get; set; }
    }

    However I can't save the List<int> in the CharacterColor class to the database, doing some reading I read that Entity Framework doesn't support lists of primitive types, so I thought the soloution would be to create another class called Color and then change the list<int> in the CharaterColor class to a List<Color>

    public class CharacterColor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Color> Colors { get; set; }
    }
    
    public class Color
    {
        public int Id { get; set; }
        public int Value{ get; set; }        
    }

    However I recieve the error message "Could not cast or convert from System.Int64 to ORAC.Data.Entities.Color"

    Anyone able to offer a soloution? What is the correct way to handle list<int> with Entity Framework?

    thanks





    • Edited by Grey-Walker Sunday, December 16, 2018 10:20 PM
    Sunday, December 16, 2018 10:19 PM

All replies

  • Hi Grey-Walker,

    As the error message mentioned, We could not cast an Type int to class Color. So we could convert int to color via programming. like this:

    //data come from JSON
    CharacterColor cc = new CharacterColor();
    //related code 
    
    //data come from database
    CharacterColorD ccd = new CharacterColorD();
    //other code
    
    ccd.Name = cc.name;
    ccd.Colors.Clear();
    foreach (var item in cc.colors)
    {
       Color c = new Color() { Value = item };
       ccd.Colors.Add(c);
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 17, 2018 1:45 AM
    Moderator
  • However I can't save the List<int> in the CharacterColor class to the database, doing some reading I read that Entity Framework doesn't support lists of primitive types,

    That doesn't make sense. EF is not concerned about a list of primitive types. It is what you are doing with the primitive type that is the potential problem.

    so I thought the soloution would be to create another class called Color and then change the list<int> in the CharaterColor class to a List<Color>

    However I recieve the error message "Could not cast or convert from System.Int64 to ORAC.Data.Entities.Color"

    Anyone able to offer a soloution? What is the correct way to handle list<int> with Entity Framework?

    It comes down to you understanding basic Object Oriented principles used by any OO language platform such as .NET or Java.

    A class is a blueprint for an object which defines fields, properties and methods/behavior for any given object. 

    Integer has its class that defines it. It doesn.t matter if int16, int32 or int64 they all have a class blueprint that defines them.

    https://docs.microsoft.com/en-us/dotnet/api/system.int32?view=netframework-4.7.2

    Then you made a Color class that is supposed to have its fields. properties and methods.

    So with this in mind about a class, the blueprint, that defines the object, you cannot tell an object that it's going to be another object without going through some kind of conversation process. You can't tell an integer object that it's going to harmoniously be a Color object, becuase the classes for each object are different, the plueprints

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    So the problem is with your basic understanding of how objects work in OO programming.

     


    • Edited by DA924x Monday, December 17, 2018 6:39 AM
    Monday, December 17, 2018 6:38 AM