示例#1
0
        private string GetTableName(MappingEntity entity, TableBaseAttribute attr)
        {
            string name = (attr != null && !string.IsNullOrEmpty(attr.Name))
                ? attr.Name
                : entity.TableId;

            return(name);
        }
示例#2
0
 internal AttributeMappingTable(AttributeMappingEntity entity, TableBaseAttribute attribute)
 {
     this.entity    = entity;
     this.attribute = attribute;
 }
示例#3
0
        private EntityModel BuildEntityModel()
        {
            // TODO: add logging

            var model = new EntityModel();

            TableBaseAttribute attribute = _type.GetCustomAttribute <TableAttribute>();

            if (attribute == null)
            {
                attribute = _type.GetCustomAttribute <ViewAttribute>();
            }

            model.HasIdentityColumn = attribute?.HasIdentityColumn ?? true;

            if (attribute is ViewAttribute viewAttribute)
            {
                model.IsView          = true;
                model.ProcedureSchema = viewAttribute.ProcedureSchema;
                model.InsertProcedure = viewAttribute.InsertProcedure;
                model.UpdateProcedure = viewAttribute.UpdateProcedure;
                model.DeleteProcedure = viewAttribute.DeleteProcedure;
            }

            if (attribute != null)
            {
                model.TableName = string.Empty;
                if (attribute.Schema != null)
                {
                    model.TableName += attribute.Schema + "].[";
                }

                if (attribute.Name != null)
                {
                    model.TableName += attribute.Name;
                }
                else
                {
                    model.TableName += _type.Name;
                }
            }
            else
            {
                model.TableName = _type.Name;
            }

            var properties = GetProperties();

            model.Columns = new List <ColumnModel>();

            foreach (var property in properties)
            {
                var columnAttribute = property.GetCustomAttribute <ColumnAttribute>();

                var primaryKeyAttribute = property.GetCustomAttribute <PrimaryKeyAttribute>();
                if (primaryKeyAttribute != null)
                {
                    model.PrimaryKeyProperty = property;

                    if (columnAttribute?.Name != null)
                    {
                        model.PrimaryKeyName = columnAttribute.Name;
                    }
                    else
                    {
                        model.PrimaryKeyName = property.Name;
                    }
                }
                else
                {
                    var columnName = property.Name;

                    if (columnAttribute?.Name != null)
                    {
                        columnName = columnAttribute.Name;
                    }

                    model.Columns.Add(new ColumnModel()
                    {
                        ColumnName = columnName,
                        Property   = property
                    });
                }
            }

            if (model.PrimaryKeyProperty == null)
            {
                throw new InvalidEntityClassException("Specified entity class is not in the expected format. Make sure the class contains one property marked with a PrimaryKeyAttribute.");
            }

            return(model);
        }