public void SaveChanges() { // A set of entities which have been visited by the iteration operation, but may not // have been added to the SQL command. This is primarily to prevent infinite-recursion scenarios. HashSet <IEntity> visited = new HashSet <IEntity>(); // A set of entities which have been added to the SQL command. HashSet <IEntity> processed = new HashSet <IEntity>(); using (var connection = new SqlConnection(_connectionString)) { connection.Open(); var transaction = connection.BeginTransaction(); var command = connection.CreateCommand(); command.Connection = connection; command.Transaction = transaction; try { // Detele all deleted link entities first. This should be ok because nothing // should be pointing at the link entities. LinkTypes.ForEach(x => x.DeleteEntities(command)); // Perform inserts, updates, and deletes of regular entities. foreach (var type in EntityTypes) { foreach (var entity in type.Container.NewEntities) { InsertEntity(command, entity, type, visited, processed); } foreach (var entity in type.Container.ChangedEntities) { UpdateEntity(command, entity, type, visited, processed); } foreach (var entity in type.Container.DeletededEntities) { DeleteEntity(command, entity, type, visited, processed); } } // Now insert all link entities, which again should be ok since nothing should be linking to them. LinkTypes.ForEach(x => x.InsertEntities(command)); transaction.Commit(); // Now that it's been committed, clear all changes. EntityTypes.ForEach(x => x.Container.ClearChanges()); LinkTypes.ForEach(x => x.ClearChanges()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); transaction.Rollback(); } } }
internal void ImportMetadata(JNode jNode, bool isFromServer) { DeserializeFrom(jNode, isFromServer); EntityTypes.ForEach(et => { // cross entity/complex type fixup. et.UpdateNavigationProperties(); et.ComplexProperties .Where(cp => cp.ComplexType == null) .ForEach(cp => cp.ComplexType = GetComplexType(cp.ComplexType.Name)); }); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { // http://www.learnentityframeworkcore.com/ // don't need this code. //modelBuilder.Entity<Bundle>().ForSqlServerToTable("Bundles"); //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); EntityTypes.ForEach(type => { var type1 = modelBuilder.Model.FindEntityType(type); if (type1 == null) { modelBuilder.Model.AddEntityType(type); } }); base.OnModelCreating(modelBuilder); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { EntityTypes.ForEach(type => { var type1 = modelBuilder.Model.FindEntityType(type); if (type1 == null) { modelBuilder.Model.AddEntityType(type); } if (type.GetCustomAttributes(typeof(HasNoKeyAttribute)).Any()) { modelBuilder.Entity(type).HasNoKey(); } }); base.OnModelCreating(modelBuilder); }
internal void ImportMetadata(JNode jNode) { DeserializeFrom(jNode); EntityTypes.ForEach(et => ResolveComplexTypeRefs(et)); }