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 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 { get; set; }
public List characterColors { get; set; }
public List wardrobeSet { get; set; }
public string raceAnimatorController { get; set; }
}
However I can't save the List 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 in the CharaterColor class to a List
public class CharacterColor
{
public int Id { get; set; }
public string Name { get; set; }
public List 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 with Entity Framework?
thanks
解决方案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