예제 #1
0
 /// <summary>
 /// 懒加载 RefTypeMeta 属性。
 /// </summary>
 private void InitLazyRefTypeMeta()
 {
     if (this._RefTypeMeta == null)
     {
         this._RefTypeMeta = CommonModel.Entities.Find(this.RefType);
     }
 }
예제 #2
0
        private static void MapDefaultColumns(EntityMeta meta)
        {
            if (meta.TableMeta != null)
            {
                foreach (var ep in meta.EntityProperties)
                {
                    var mp = ep.ManagedProperty;
                    //Id 属性,默认的元数据中,即是主键,也是自增长列。应用层可以考虑再做配置。
                    if (mp == EntityConvention.Property_Id)
                    {
                        ep.ColumnMeta = new ColumnMeta
                        {
                            ColumnName   = mp.Name,
                            IsPrimaryKey = true,
                        };
                        if (meta.IdType == typeof(int) || meta.IdType == typeof(long))
                        {
                            ep.ColumnMeta.IsIdentity = true;
                        }
                    }
                    //其它属性
                    else if (mp != EntityConvention.Property_TreeIndex && mp != EntityConvention.Property_TreePId)
                    {
                        var clrProperty = ep.CLRProperty;
                        if (clrProperty != null)
                        {
                            var columnAttri = clrProperty.GetSingleAttribute <ColumnAttribute>();
                            if (columnAttri != null)
                            {
                                var name = columnAttri.ColumnName;
                                if (string.IsNullOrWhiteSpace(name))
                                {
                                    name = clrProperty.Name;
                                }

                                ep.ColumnMeta = new ColumnMeta {
                                    ColumnName = name
                                };
                            }
                        }
                    }
                }

                //树的两个属性
                if (meta.IsTreeEntity)
                {
                    var p = meta.Property(EntityConvention.Property_TreeIndex);
                    if (p != null)
                    {
                        p.MapColumn();
                    }

                    p = meta.Property(EntityConvention.Property_TreePId);
                    if (p != null)
                    {
                        p.MapColumn();
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 打开指定实体类型的树型功能
        /// </summary>
        /// <param name="meta"></param>
        /// <returns></returns>
        public static EntityMeta SupportTree(this EntityMeta meta)
        {
            meta.IsTreeEntity = true;

            MapDefaultColumns(meta);

            return(meta);
        }
예제 #4
0
        /// <summary>
        /// 指定某实体映射某个表。
        /// </summary>
        /// <param name="meta"></param>
        /// <returns></returns>
        public static EntityMeta MapTable(this EntityMeta meta)
        {
            meta.TableMeta = new TableMeta(meta.EntityType.Name);

            MapDefaultColumns(meta);

            return(meta);
        }
예제 #5
0
        /// <summary>
        /// 指定某实体映射某个表,指定表名。
        /// </summary>
        /// <param name="meta"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static EntityMeta MapTable(this EntityMeta meta, string tableName)
        {
            meta.TableMeta = new TableMeta(tableName);

            MapDefaultColumns(meta);

            return(meta);
        }
예제 #6
0
        /// <summary>
        /// 指定某实体映射某个视图 SQL。
        /// </summary>
        /// <param name="meta"></param>
        /// <param name="viewSql">
        /// 可以是一个数据库视图,也可以是一个能查询出数据的 Sql 语句。
        ///
        /// 如果不指定此参数,则需要在所有的查询中都编写自定义查询。
        /// </param>
        /// <returns></returns>
        public static EntityMeta MapView(this EntityMeta meta, string viewSql = null)
        {
            meta.TableMeta         = new TableMeta();
            meta.TableMeta.ViewSql = viewSql;

            MapDefaultColumns(meta);

            return(meta);
        }
예제 #7
0
        /// <summary>
        /// 指定该实体类型中的某些属性直接映射数据库字段
        /// </summary>
        /// <param name="meta"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static EntityMeta MapProperties(this EntityMeta meta, params IManagedProperty[] properties)
        {
            foreach (var p in properties)
            {
                var ep = meta.Property(p);
                ep.MapColumn();
            }

            return(meta);
        }
예제 #8
0
        /// <summary>
        /// 指定所有属性全部映射数据库字段
        /// </summary>
        /// <param name="meta"></param>
        /// <returns></returns>
        public static EntityMeta MapAllProperties(this EntityMeta meta)
        {
            var properties = meta.ManagedProperties.GetNonReadOnlyCompiledProperties()
                             .Where(p => !(p is IListProperty) && !(p is IRefEntityProperty) &&
                                    p != EntityConvention.Property_TreeIndex &&
                                    p != EntityConvention.Property_TreePId
                                    ).ToArray();

            return(meta.MapProperties(properties));
        }
예제 #9
0
        /// <summary>
        /// 指定某实体映射某个表,指定表名。
        /// </summary>
        /// <param name="meta"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static EntityMeta MapTable(this EntityMeta meta, string tableName)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            meta.TableMeta = new TableMeta(tableName);

            MapDefaultColumns(meta);

            return(meta);
        }
예제 #10
0
        /// <summary>
        /// 指定某实体映射某个虚拟的视图。
        /// 当映射视图时,不会生成数据库表,仓库中也需要在所有的查询中都编写自定义查询。
        /// </summary>
        /// <param name="meta">The meta.</param>
        /// <param name="mapDefaultProperties">设置为 true,则会自动映射 Id、TreeId、TreePId 以及有 ColumnAttribute 标签的属性。</param>
        /// <returns></returns>
        ///// <param name="viewSql">
        ///// 可以是一个数据库视图,也可以是一个能查询出数据的 Sql 语句。
        /////
        ///// 如果不指定此参数,则需要在所有的查询中都编写自定义查询。
        ///// </param>
        public static EntityMeta MapView(this EntityMeta meta, bool mapDefaultProperties = true)//, string viewSql = null)
        {
            meta.TableMeta = new TableMeta(meta.EntityType.Name);
            meta.TableMeta.IsMappingView = true;
            //meta.TableMeta.ViewSql = viewSql;

            if (mapDefaultProperties)
            {
                MapDefaultColumns(meta);
            }

            return(meta);
        }
예제 #11
0
        /// <summary>
        /// 指定某个实体的所有属性全部映射数据库字段
        /// 同时排除指定的属性列表。
        /// </summary>
        /// <param name="meta"></param>
        /// <param name="exceptProperties">
        /// 这些属性不需要映射数据库
        /// </param>
        /// <returns></returns>
        public static EntityMeta MapAllPropertiesExcept(this EntityMeta meta, params IManagedProperty[] exceptProperties)
        {
            IEnumerable <IManagedProperty> properties = meta.ManagedProperties.GetNonReadOnlyCompiledProperties()
                                                        .Where(p => !(p is IListProperty) && !(p is IRefEntityProperty) &&
                                                               p != EntityConvention.Property_TreeIndex &&
                                                               p != EntityConvention.Property_TreePId &&
                                                               p != EntityConvention.Property_IsPhantom
                                                               );

            if (exceptProperties != null)
            {
                properties = properties.Except(exceptProperties);
            }

            return(meta.MapProperties(properties.ToArray()));
        }
예제 #12
0
 /// <summary>
 /// 通过托管属性获取所有的扩展属性列表
 /// </summary>
 /// <param name="em"></param>
 /// <returns></returns>
 internal static IList <IManagedProperty> GetEntityPropertiesExtension(EntityMeta em)
 {
     return(em.ManagedProperties.GetCompiledProperties().Where(mp => mp.IsExtension).ToArray());
 }
예제 #13
0
 /// <summary>
 /// 指定所有属性全部映射数据库字段
 /// </summary>
 /// <param name="meta"></param>
 /// <returns></returns>
 public static EntityMeta MapAllProperties(this EntityMeta meta)
 {
     return(MapAllPropertiesExcept(meta, null));
 }
예제 #14
0
 /// <summary>
 /// 通过托管属性获取所有的实体属性列表
 /// </summary>
 /// <param name="entityMeta"></param>
 /// <returns></returns>
 internal static IList <IManagedProperty> GetEntityProperties(EntityMeta entityMeta)
 {
     return(entityMeta.ManagedProperties.GetCompiledProperties()
            .Where(mp => !mp.IsExtension && !(mp is IListProperty))
            .ToArray());
 }
예제 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EntityMetaCreatedEventArgs"/> class.
 /// </summary>
 /// <param name="meta">The meta.</param>
 internal EntityMetaCreatedEventArgs(EntityMeta meta)
 {
     this.EntityMeta = meta;
 }
예제 #16
0
 /// <summary>
 /// 通过托管属性获取所有的子属性列表
 /// </summary>
 /// <param name="entityMeta"></param>
 /// <returns></returns>
 internal static IList <IManagedProperty> GetChildrenProperties(EntityMeta entityMeta)
 {
     return(entityMeta.ManagedProperties.GetCompiledProperties()
            .Where(mp => mp is IListProperty)
            .ToArray());
 }
예제 #17
0
 internal void UseDefaultMeta()
 {
     this._meta = null;
 }