怎么把集合里面的值存入mysql,实体框架如何将类型为int的List保存到数据库中

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值