Inheritance: System.Attribute
Example #1
0
        //把LinqToSql的实体映射翻译为ELinq的实体映射,然后再把该映射注册到DbConfiguration 中
        private void AddDLinqClass(Type entityType, Attribute dlinqTableAttribute)
        {
            var mapping = new ClassMap {
                EntityType = entityType
            };
            var table = new TableAttribute();

            mapping.table = table;

            var tableName = DLinq.Instance.Table.Name(dlinqTableAttribute) as string;

            if (!string.IsNullOrEmpty(tableName))
            {
                var parts = tableName.Split('.').Where(p => !string.IsNullOrEmpty(p)).ToArray();
                switch (parts.Length)
                {
                case 1:
                    table.Name = tableName;
                    break;

                case 2:
                    table.Schema = parts[0];
                    table.Name   = parts[1].TrimStart('[').TrimEnd(']');
                    break;

                case 3:
                    table.DatabaseName = parts[0];
                    table.Schema       = parts[1];
                    table.Name         = parts[2].TrimStart('[').TrimEnd(']');
                    break;

                case 4:
                    table.Server       = parts[0];
                    table.DatabaseName = parts[1];
                    table.Schema       = parts[2];
                    table.Name         = parts[3].TrimStart('[').TrimEnd(']');
                    break;

                default:
                    throw new MappingException("Invalid table name '" + tableName + "'");
                }
            }

            foreach (var p in entityType.GetProperties().Cast <MemberInfo>()
                     .Union(entityType.GetFields().Cast <MemberInfo>()))
            {
                //Column
                var c = p.GetCustomAttributes(DLinq.Instance.Column.Type, false).FirstOrDefault();
                if (c != null)
                {
                    PopulateDLinqColumn(entityType, mapping, p, c);
                    continue;
                }

                var a = p.GetCustomAttributes(DLinq.Instance.Association.Type, false).FirstOrDefault();
                if (a != null)
                {
                    PopulateDLinqAssociation(mapping, p, a);
                    continue;
                }
            }

            var entity = mapping.CreateMapping();

            Guard.NotNull(entity, "entity");

            AutoMapping(entity.entityType, entity);

            RegistyMapping(entity);
        }