Ejemplo n.º 1
0
        private static void FillRelations(this IModel model, PropertyInfo property)
        {
            var attribute = (ForeignKeyAttribute)property
                            .GetCustomAttributes(typeof(ForeignKeyAttribute))
                            .First();
            var relations = ORM.GetRelations(model, attribute.Type);

            property.SetValue(model, new DbSet(model, Data[attribute.Type]
                                               .Where(m => relations.Contains(m.Id))));
        }
Ejemplo n.º 2
0
        public static void SaveChanges()
        {
            var models = Data.Values.SelectMany(e => e).ToList();

            foreach (var model in models)
            {
                if (model.IsNew)
                {
                    ORM.Insert(model);
                    model.IsNew = false;
                }

                if (model.IsChanged)
                {
                    ORM.Update(model);
                    model.IsChanged = false;
                }

                if (model.IsDeleted)
                {
                    ORM.Delete(model);
                    DeleteModel(model);
                }
            }

            var dbSets = models.SelectMany(m => m.GetType()
                                           .GetRelationProperties()
                                           .Select(p => (DbSet)p.GetValue(m)));

            foreach (var dbSet in dbSets)
            {
                if (dbSet == null)
                {
                    continue;
                }

                foreach (var model in dbSet.NewModels)
                {
                    ORM.AddRelation(dbSet.Parent, model);
                }
                dbSet.NewModels.Clear();

                foreach (var model in dbSet.RemovedModels)
                {
                    ORM.DeleteRelation(dbSet.Parent, model);
                }
                dbSet.RemovedModels.Clear();
            }
        }