示例#1
0
        public ReferenceMap(EntityProperty property, IEnumerable <EntityProperty> properties, RelationalMapper mapper)
        {
            this.Property = property;
            _mapper       = mapper;
            mapper.AddMap(this.Property.PropertyType, () => new EntityMap(property.PropertyType, mapper));

            if (!typeof(IEnumerable <>).IsAssignableFrom(property.PropertyType))
            {
                var fka = (ForeignKeyAttribute)property.Attributes.SingleOrDefault(a => a is ForeignKeyAttribute);
                if (fka != null)
                {
                    // Check if it's a multi key.
                    var keys = fka.Name.Split(',').Select(n => n.Trim());
                    this.ForeignKeys = properties.Where(p => keys.Contains(p.Name));
                }
                else
                {
                    // Get the foreign keys that are referencing this property.
                    this.ForeignKeys = properties.Where(p => p.Attributes.Any(a => a is ForeignKeyAttribute && ((ForeignKeyAttribute)a).Name.Equals(property.Name))).OrderBy(p => (p.Attributes.SingleOrDefault(a => a is ColumnAttribute) as ColumnAttribute)?.Order ?? 0);
                }
            }
            else
            {
                this.ForeignKeys = properties.Where(p => p.Attributes.Any(a => a is KeyAttribute)).OrderBy(p => (p.Attributes.SingleOrDefault(a => a is ColumnAttribute) as ColumnAttribute)?.Order ?? 0);
            }
        }
示例#2
0
        public EntityMap(Type type, RelationalMapper mapper)
        {
            this.EntityType = type?.GetType() ?? throw new ArgumentNullException(nameof(type));
            var properties = this.EntityType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.GetCustomAttribute <NotMappedAttribute>() == null).Select(p => new EntityProperty(p));

            this.PrimaryKeys = properties.Where(p => p.Attributes.Any(a => a is KeyAttribute)).Select(p => new EntityKey(p)).OrderBy(k => k.Order);
            this.Properties  = properties.Where(p => p.PropertyType.IsValueType || p.PropertyType == typeof(string)); // TODO: Need to handle all types.
            var values = this.Properties;

            this.References  = properties.Where(p => p.PropertyType.IsClass).Select(p => new ReferenceMap(p, values, mapper));
            this.Collections = properties.Where(p => typeof(IEnumerable <>).IsAssignableFrom(p.PropertyType)).Select(p => new ReferenceMap(p, values, mapper));
        }