ISaveable<T> 没有 AS 方法进行表别名的映射, 个人给一个解决方案,不知道可不可行 返回

反馈
1 144
该叫什么 wynnyo 发布于2周前
悬赏:5 飞吻
public partial class SaveableProvider<T> : ISaveable<T> where T : class, new()
{
        // ......
        public bool IsAs { get; set; }
        public string TableName { get; set; }
        // ......

       // 新增 AS 方法
        public ISaveable<T> AS(string tableName)
        {
            IsAs = true;
            TableName = tableName;
            return this; ;
        }

        // 修改 LoadInsertable 方法
        private void LoadInsertable()
        {
            var temp = insertObjects;
            if (insertable == null && temp.HasValue())
            {
                insertable = this.Context.Insertable<T>(temp);
                if (IsAs)
                    insertable = insertable.AS(TableName);
            }
                
        }
        // 修改 LoadInsertable 方法
        private void LoadUpdateable()
        {
            var temp = updatObjects;
            if (updateable == null && temp.HasValue())
            {
                updateable = this.Context.Updateable<T>(temp);
                if (IsAs)
                    updateable = updateable.AS(TableName);
            }
        }
}


热忱回答1

  • public List<T> insertObjects
            {
                get
                {
                    var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
                    this.Context.Ado.IsDisableMasterSlaveSeparation = true;
                    List<T> result = new List<T>();
                    var pks = GetPrimaryKeys();
                    Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
                    Check.Exception(pks.Count() > 1, "Multiple primary keys are not supported");
                    var pkInfo = this.EntityInfo.Columns.Where(it=>it.IsIgnore==false).Where(it => it.DbColumnName.Equals(pks.First(), StringComparison.CurrentCultureIgnoreCase)).First();
                    var pkValues = saveObjects.Select(it=>it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it,null));
                    if (existsObjects == null)
                    {
                        var queryable = this.Context.Queryable<T>();
                        if (IsAs)
                        {
                            queryable = queryable.AS(TableName);
                        }
    
                        existsObjects = queryable.In(pkValues).ToList();
                    }
              
                    this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
                    return saveObjects.Where(it=>!
                    existsObjects.Any(e=>
                                     e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e,null).ObjToString()
                                     == 
                                    it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null).ObjToString())).ToList();
                }
            }

    还需要 改 insertObjects 和 updatObjects 就应该正常了

    0 回复

版块

学习文档

最新会员

发布达人

回贴达人