public RFContext Create()
        {
            //return new RFContext(_options);

            var context = new RFContext(_options);

            context.Database.Migrate();
            var newlyCreated = context.Database.EnsureCreated();

            if (newlyCreated)
            {
                context.Seed();
            }

            return(context);

            //var optionsBuilder = new DbContextOptionsBuilder<RFContext>();
            //optionsBuilder.UseMySQL(_connectionString);

            //var context = new RFContext(optionsBuilder.Options);
            //var newlyCreated = context.Database.EnsureCreated();

            //if (newlyCreated)
            //{
            //	context.Seed();
            //}

            //return context;
        }
Esempio n. 2
0
        /// <summary>
        /// Currently Find is missing from the Entity framework Core API.
        /// Fix taken from: http://stackoverflow.com/questions/29030472/dbset-doesnt-have-a-find-method-in-ef7
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="set"></param>
        /// <param name="context"></param>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static TEntity Find <TEntity>(this IQueryable <TEntity> set, RFContext context, params object[] keyValues) where TEntity : class
        {
            var entityType = context.Model.FindEntityType(typeof(TEntity));
            var key        = entityType.FindPrimaryKey();

            var entries = context.ChangeTracker.Entries <TEntity>();

            var i = 0;

            foreach (var property in key.Properties)
            {
                var i1 = i;
                entries = entries.Where(e => e.Property(property.Name).CurrentValue == keyValues[i1]);
                i++;
            }

            var entry = entries.FirstOrDefault();

            if (entry != null)
            {
                // Return the local object if it exists.
                return(entry.Entity);
            }

            var parameter = Expression.Parameter(typeof(TEntity), "x");
            var query     = set.AsQueryable();

            i = 0;
            foreach (var property in key.Properties)
            {
                var i1 = i;
                query = query.Where((Expression <Func <TEntity, bool> >)
                                    Expression.Lambda(
                                        Expression.Equal(
                                            Expression.Property(parameter, property.Name),
                                            Expression.Constant(keyValues[i1])),
                                        parameter));
                i++;
            }

            // Look in the database
            return(query.FirstOrDefault());
        }
        public static void Seed(this RFContext context)
        {
            if (context.AllMigrationsApplied())
            {
                var didMakeModification = false;
                var serialNumber        = new SerialNumber {
                    Number = 0
                };

                if (!context.Scenarios.Any(s => s.SerialNumber == serialNumber.Number))
                {
                    context.SerialNumbers.Add(serialNumber);
                    didMakeModification = true;
                }

                var adminUser = new User
                {
                    Id                = 0,
                    Username          = "******",
                    Password          = "******",
                    MemberType        = "admin",
                    ContentRegions    = "[ \"Lancashire\", \"LondonMetDemo\"]",
                    ValidationRegions = "[ \"Lancashire\", \"LondonMetDemo\"]"
                };

                if (!context.Users.Any(u => u.Username == adminUser.Username && u.Password == adminUser.Password))
                {
                    context.Users.Add(adminUser);
                    didMakeModification = true;
                }

                if (didMakeModification)
                {
                    context.SaveChanges();
                }
            }
        }