Example #1
0
        private static void ApplyChanges <TEntity>(TEntity root) where TEntity : class, IObjectWithState
        {
            using (var context = new BreakAwayContext())
            {
                context.Set <TEntity>().Add(root);

                CheckForEntitiesWithoutStateInterface(context);

                foreach (var entry in context.ChangeTracker
                         .Entries <IObjectWithState>())
                {
                    IObjectWithState stateInfo = entry.Entity;
                    if (stateInfo.State == State.Modified)
                    {
                        entry.State = EntityState.Unchanged;
                        foreach (var property in stateInfo.ModifiedProperties)
                        {
                            entry.Property(property).IsModified = true;
                        }
                    }
                    else
                    {
                        entry.State = ConverterState(stateInfo.State);
                    }
                    entry.State = ConverterState(stateInfo.State);
                }

                context.SaveChanges();
            }
        }
        private static void UseEdmMetadataTable()
        {
            using (var context = new BreakAwayContext())
            {
                var modelHash = EdmMetadata.TryGetModelHash(context);
                Console.WriteLine("Current Model Hash: {0}", modelHash);

                var databaseHash = context.Set <EdmMetadata>().Single().ModelHash;
                Console.WriteLine("Current Database Hash: {0}", databaseHash);

                var compatible = context.Database.CompatibleWithModel(throwIfNoMetadata: true);
                Console.WriteLine("Model Compatible With Database?: {0}", compatible);
            }
        }
        private static void InsertDestination()
        {
            var destination = new Destination
            {
                Country     = "Indonesia",
                Description = "EcoTourism at its best in exquisiste Bali",
                Name        = "Bali"
            };

            using (var context = new BreakAwayContext())
            {
                context.Destinations.Add(destination);
                context.SaveChanges();
                Console.WriteLine(context.Set <Destination>().FirstOrDefault().Description);
                Console.WriteLine(context.Database.Connection.ConnectionString);
            }
        }
        private static void ApplyChanges <TEntity>(TEntity root)
            where TEntity : class, IObjectWithState
        {
            using (var context = new BreakAwayContext())
            {
                context.Set <TEntity>().Add(root);
                CheckForEntitiesWithoutStateInterface(context);

                foreach (var entry in context.ChangeTracker.Entries <IObjectWithState>())
                {
                    IObjectWithState stateInfo = entry.Entity;
                    entry.State = ConvertState(stateInfo.State);
                    if (stateInfo.State == State.Unchanged)
                    {
                        var databaseValues = entry.GetDatabaseValues();
                        entry.OriginalValues.SetValues(databaseValues);
                    }
                }

                context.SaveChanges();
            }
        }