c code first mysql_Code First 中 mysql如何存list或Dictionary对象

你好,

根据我的研究以及搜索,我重现了您的问题。如果您使用的是EntityFrmework,那么我推荐您的使用EntityFramework-core,这样你可以用下面的方式来存储您的list或者Dictionary对象。

[Table("Fitting")]

public class Fitting

{

[Key]

public int Id { get; set; }

public string Name { get; set; }

public double Weight { get; set; }

public virtual Dictionary IceWeightList { get; set; }

}

public class MyContext : DbContext

{

public DbSet Customers { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

optionsBuilder

.UseSqlServer(@"connstring");

}

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

modelBuilder.Entity().Property(e => e.IceWeightList).HasConversion(v => JsonConvert.SerializeObject(v),

v => JsonConvert.DeserializeObject>(v));

}

}

在执行完Add-Migration操作后,你会得到:

protected override void Up(MigrationBuilder migrationBuilder)

{

migrationBuilder.CreateTable(

name: "Fitting",

columns: table => new

{

Id = table.Column(type: "int", nullable: false)

.Annotation("SqlServer:Identity", "1, 1"),

Name = table.Column(type: "nvarchar(max)", nullable: true),

Weight = table.Column(type: "float", nullable: false),

IceWeightList = table.Column(type: "nvarchar(max)", nullable: true)

},

constraints: table =>

{

table.PrimaryKey("PK_Fitting", x => x.Id);

});

}

注意:完成上面的操作,必须要保证当前的项目是.net core app,还有就是要添加下面的三个nuget 包。

64bae57011bde45717d493356c3b8059.png

希望能够帮助你。

Best Regards,

Jack

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值