예제 #1
0
        /// <summary>
        /// Creates a readonly login and user in the target database.
        /// </summary>
        public static void CreateReadonlyUser(this DbContext context, DbReadonlyUser readonlyUser)
        {
            var createUserCmd = $"CREATE USER [{readonlyUser.UserName}] FOR LOGIN [{readonlyUser.LoginName}]";

            ExecuteNonQuery(context.Database.Connection.ConnectionString, createUserCmd);

            var addRoleToUserCmd = $"EXEC sp_addrolemember N'db_datareader', N'{readonlyUser.UserName}'";

            ExecuteNonQuery(context.Database.Connection.ConnectionString, addRoleToUserCmd);
        }
        public void InitializeDatabase(
            TDbContext context,
            int dbSizeInGB,
            string edition,
            string serviceObjective,
            DbReadonlyUser readonlyUser = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (context.Database.Exists())
            {
                if (context.Database.CompatibleWithModel(true))
                {
                    return;
                }
                context.Database.Delete();
            }

            if (context.IsAzureDatabase())
            {
                context.CreateAzureDatabase(dbSizeInGB, edition, serviceObjective);
                if (readonlyUser != null)
                {
                    context.CreateReadonlyUser(readonlyUser);
                }
            }
            else
            {
                context.Database.Create();
            }

            context.SaveChanges();
        }
예제 #3
0
        public static void CreateReadonlyUser(this DbContext context, DbReadonlyUser readonlyUser)
        {
            var createUserCmd = $"CREATE USER [{readonlyUser.UserName}] FOR LOGIN [{readonlyUser.LoginName}]";
            ExecuteNonQuery(context.Database.Connection.ConnectionString, createUserCmd);

            var addRoleToUserCmd = $"EXEC sp_addrolemember N'db_datareader', N'{readonlyUser.UserName}'";
            ExecuteNonQuery(context.Database.Connection.ConnectionString, addRoleToUserCmd);
        }