db.CodeFirst.GetDifferenceTables 未判断索引 返回

SqlSugar 处理完成
5 105
该叫什么 joyswing 发布于2周前
悬赏:0 飞吻

数据库: SqlServer


实体对象:


    [SugarTable("BS_Org")]
    [SugarIndex("ui_{table}_cd", nameof(Org.OrgCD), OrderByType.Asc, true)]
    //[SugarIndex("ui_org_name", nameof(Org.OrgName), OrderByType.Asc)]
    public class Org
    {

        [SugarColumn(ColumnName = "Org_ID", IsPrimaryKey = true)]
        [JsonConverter(typeof(IdToStringConverter))]
        public long OrgId { get; set; }

        [SugarColumn(ColumnName = "Org_CD")]
        public string OrgCD { get; set; }

        [SugarColumn(ColumnName = "Memo_TX", IsNullable = true)]
        public string Memo { get; set; }

        [SugarColumn(ColumnName = "OrgName_TX")]
        public string OrgName { get; set; }

        [SugarColumn(ColumnName = "Property_ID", IsNullable = true)]
        public int PropertyId { get; set; }

        [SugarColumn(ColumnName = "SortNo_NR", DefaultValue = "0", IsNullable = true)]
        public int? SortNo { get; set; }

        [SugarColumn(ColumnName = "AddUser_TX", IsNullable = true)]
        public string AddUser { get; set; }

        [SugarColumn(ColumnName = "Add_DT", IsNullable = true)]
        public DateTime? AddDT { get; set; }

        [SugarColumn(ColumnName = "EditUser_TX", IsNullable = true)]
        public string EditUser { get; set; }

        [SugarColumn(ColumnName = "Edit_DT", IsNullable = true)]
        public DateTime? EditDT { get; set; }

        [SugarColumn(ColumnName = "Active_YN", IsNullable = true)]
        public string ActiveYN { get; set; }

        [SugarColumn(ColumnName = "WorkDateVersion_TX", IsNullable = true)]
        public string WorkDateVersion { get; set; }

        [SugarColumn(ColumnName = "InterFaceKey_CD", IsNullable = true)]
        public string InterfaceKeyCD { get; set; }

        public object Adapt<T1, T2>(Action<object> value)
        {
            throw new NotImplementedException();
        }
    }


代码:


static bool HasDiff(SqlSugarScope db, Type type)
{

    var list = db.CodeFirst.GetDifferenceTables(type).ToDiffList();
    return list.Any();
}


static void TestOrg()
{
    try
    {
        var db = SqlSugarHelper.Db;

        var hasDiff = HasDiff(db, typeof(Org));

        if (hasDiff)
        {
            Console.WriteLine("no diff");
            return;
        }

        db.CodeFirst.InitTables(typeof(Org));
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

现象:本来想给表Org添加索引,但是执行HasDiff的时候,返回false,跳过去了。demo参见附件

dbtest.zip


热忱回答5

  • 索引不会判断差异

    0 回复
  • 以实体为准

    0 回复
  • GetDifferenceTables原本的含义,就是没有差异,就不需要同步表结构,索引相关的,但是现在这个失去作用了,

    碰到我这种需求,有什么新函数可以判断索引发生了变化?

    0 回复
  • @joyswing:目前没有对比索引这个功能,因为结构对比只是对数据结构的控制,不存在删索引的需求

    只有添加索引和验证索引是不是存在。没有删和修改。

    0 回复
  • 索引的内部也没办法验证他的内容是什么东西

    0 回复