none
Code First 中 mysql如何存list或Dictionary对象 RRS feed

  • 问题

  • public  class Fitting 
        {
            public string Name { get; set; }
            public double Weight { get; set; }
            public Dictionary<string, double> IceWeightList { get; set; }  

      }  

    Code First 进行开发时,PM> Add-Migration iceEnty 生成代码如下


                CreateTable(
                    "dbo.Fittings",
                    c => new
                        {                      
                            NAME = c.String(nullable: false, unicode: false),
                            WEIGHT = c.Double(nullable: false),                      
                             });

    //IceWeightList 这个属性不会进行创建,请问如何手动创建

     
    2021年1月30日 1:55

全部回复

  • 你好,

    根据我的研究以及搜索,我重现了您的问题。如果您使用的是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<string, double> IceWeightList { get; set; }
        }
    
    
        public class MyContext : DbContext
        {
            public DbSet<Fitting> Customers { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder
                    .UseSqlServer(@"connstring");
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Fitting>().Property(e => e.IceWeightList).HasConversion(v => JsonConvert.SerializeObject(v),
                v => JsonConvert.DeserializeObject<Dictionary<string, double>>(v));
                    
            }
        }

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

    protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.CreateTable(
                    name: "Fitting",
                    columns: table => new
                    {
                        Id = table.Column<int>(type: "int", nullable: false)
                            .Annotation("SqlServer:Identity", "1, 1"),
                        Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
                        Weight = table.Column<double>(type: "float", nullable: false),
                        IceWeightList = table.Column<string>(type: "nvarchar(max)", nullable: true)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_Fitting", x => x.Id);
                    });
            }

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

    希望能够帮助你。

    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.

    2021年2月1日 3:32
    版主