Use Code First ,The primary key must not exceed 1 返回

SqlSugar 老数据
13 2482

问题如上所述,以下是我定义的实体类基类:

     [SugarColumn(IsPrimaryKey = true,ColumnDataType ="char",Length =32)]
        public string Id { get; set; }
        [SugarColumn(IsNullable = true)]
        public DateTime? CreateTime { get; set; } = DateTime.Now;


因为这个项目用的webapi,后台数据库主键全部是string(32),还请帮忙解决这个问题。


sqlite数据库

热忱回答(13)

  • 发报错的代码

    0 回复
  • 不是最新更新最新的

    0 回复
  • @fate stay night:标题就是全部的异常提示了

    0 回复
  • @fate stay night:SqlSugar.SqlSugarException:“Use Code First ,The primary key must not exceed 1”
    。更新到了最新,异常没有变

    0 回复
  • @fate stay night:我qq:1085234845.这个问题挺急的,我是全新整理的框架,用的sqlsugar,代码写完了,今天开始调,codefirst要是过不去,可就耽误大了

    0 回复
  • @fate stay night:

    Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Model.User),
                typeof(Model.Config_Menu_Children),
                typeof(Model.Config_Menu_Children),
                typeof(Model.AppIcon),
                typeof(Model.Project),
                typeof(Model.Relation_Serail_User),
                typeof(Model.Result),
                typeof(Model.Sample_Detail),
                typeof(Model.Sample_Group),
                typeof(Model.Sample_Detail),
                typeof(Model.Work_Menu),
                typeof(Model.Project_HelpFile),
                typeof(Model.Config_System),
                typeof(Model.Config_Device_DNA),
                typeof(Model.Config_Device_Project)

    0 回复
  •  public class DbContext
        {
            public DbContext()
            {
                Db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = "Data Source ="+Application.StartupPath+ @"\db\wr.db",
                    DbType = DbType.Sqlite,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                });

                Db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql + "\r\n" +
                        Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    Console.WriteLine();
                };
            }
            public SqlSugarClient Db;
        }

    0 回复
  • @fate stay night:codefirst模式是不支持string类型的主键吗?

    0 回复
  • 不支持多主键实体

    0 回复
  • 最新版本才支持

    0 回复
  • @fate stay night:每个实体都是单主键的,这个可以确定

    0 回复
  • 你看看那个实体2主键的

    0 回复
  • @fate stay night:哎呀,我犯了个错,继承的基础表里有ID,有一张表重复写了主键,谢谢谢谢

    0 回复