예제 #1
0
        public T Load <T>(object entityObj, ref T loadTo, [CallerMemberName] string name = "")
        {
            if (!ShouldLoad(loadTo, name))
            {
                return(loadTo);
            }

            var attribute = _entity.Attributes[name];
            var fkAttr    = attribute.Relation.FromKeyAttribute;

            var otherEntity = _context.Schema.EntityTypeMap[typeof(T)];

            var fk = _entry.ShadowAttributes[fkAttr.Name];

            var query = new QueryBuilder(_context)
                        .Select <T>()
                        .Where(new WhereSqlFragment(
                                   BinaryExpression.Eq(
                                       new Accessor
            {
                PropertyName = attribute.Relation.ToKeyAttribute.DataBaseColumn,
                TableName    = otherEntity.TableName
            },
                                       Value.FromAttr(fk, fkAttr)
                                       ))).Build();

            var loaded = _context.Connection.Select(query).First();

            loadTo = loaded;
            _entry.OriginalValues[attribute.Name] = loaded;
            _attributeGenerations[name]           = _context.ChangeTracker.Generation;
            return(loaded);
        }
예제 #2
0
        /// <summary>
        /// Finds a specific entity with the given primary key, returns null is the given key was not found
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public T Find(object primaryKey)
        {
            var entity = _ctx.Schema.EntityTypeMap[typeof(T)];
            var pkAttr = entity.PrimaryKeyAttribute;

            var filter = new WhereSqlFragment(
                BinaryExpression.Eq(
                    new Accessor
            {
                PropertyName = pkAttr.DataBaseColumn,
                TableName    = entity.TableName
            }, Value.FromAttr(primaryKey, pkAttr)));

            var query = new QueryBuilder(_ctx)
                        .Select <T>()
                        .Where(filter)
                        .Build();

            return(_ctx.Connection.Select(query).FirstOrDefault());
        }
예제 #3
0
        public ICollection <T> Load <T>(object entityObj, ref ICollection <T> loadTo, [CallerMemberName] string name = "")
        {
            if (!ShouldLoad(loadTo, name))
            {
                return(loadTo);
            }

            var propAttr    = _entity.Attributes[name];
            var otherEntity = _context.Schema.EntityTypeMap[typeof(T)];

            if (propAttr.Relation is ManyToMany relation)
            {
                return(LoadManyToMany(entityObj, ref loadTo, name, relation));
            }

            var attribute = _entity.Attributes[name];

            var fkAttr = attribute.Relation.ToKeyAttribute;
            var pkAttr = attribute.Relation.FromKeyAttribute;

            var pk      = pkAttr.InvokeGetter(entityObj);
            var otherPk = attribute.Relation.ToEntity.PrimaryKeyAttribute;

            var query = new QueryBuilder(_context)
                        .Select <T>()
                        .Where(new WhereSqlFragment(BinaryExpression.Eq(
                                                        new Accessor {
                PropertyName = fkAttr.DataBaseColumn, TableName = otherEntity.TableName
            },
                                                        Value.FromAttr(pk, pkAttr)
                                                        ))).Build();
            var loaded = _context.Connection.Select(query).ToList();

            loadTo = loaded;

            _entry.ReplaceRelatedKeys(attribute,
                                      loaded.Select(obj => otherPk.InvokeGetter(obj)).ToList()
                                      );
            _attributeGenerations[name] = _context.ChangeTracker.Generation;
            return(loadTo);
        }
예제 #4
0
        private ICollection <T> LoadManyToMany <T>(object entityObj, ref ICollection <T> loadTo, string propertyName,
                                                   ManyToMany relation)
        {
            var propAttr    = _entity.Attributes[propertyName];
            var otherEntity = _context.Schema.EntityTypeMap[typeof(T)];

            var thisEntity    = _context.Schema.EntityTypeMap[typeof(T)];
            var betweenEntity = relation.BetweenEntity;
            //student.id
            var otherEntityPkAttr = relation.ToKeyAttribute;
            //fk_student
            var otherEntityFkAttr = relation.ReferencedToAttribute;

            //Students
            var fromEntityAttribute = relation.FromEntityAttribute;

            var pk = relation.FromKeyAttribute.InvokeGetter(entityObj);

            var query = new SelectQueryBuilder(_context)
                        .FromTable <T>(thisEntity.TableName)
                        .Join(betweenEntity, otherEntityPkAttr, otherEntityFkAttr)
                        .Select(otherEntity.Attributes.Values
                                .Where(attr => !attr.IsEntityType)
                                .Select(attr => attr.DataBaseColumn))
                        .Where(new WhereSqlFragment(
                                   BinaryExpression.Eq(new Accessor
            {
                PropertyName = relation.ReferencedFromAttribute.DataBaseColumn,
                TableName    = betweenEntity.TableName
            }, Value.FromAttr(pk, relation.FromKeyAttribute))))
                        .Build();

            var loaded = _context.Connection.Select(query).ToList();

            loadTo = loaded;
            _entry.ReplaceRelatedKeys(propAttr, loaded.Select(obj =>
                                                              otherEntity.PrimaryKeyAttribute.InvokeGetter(obj)).ToList());
            _attributeGenerations[propertyName] = _context.ChangeTracker.Generation;
            return(loaded);
        }