public static SchemaDbContext GetContext(string connectionString) { var context = new SchemaDbContext(connectionString); // https://forums.asp.net/t/1864180.aspx?EF+returning+two+identical+records+when+2+different+records+are+in+the+table+ // For example, query of KEY_COLUMN_USAGES will return identical rows without AsNoTracking(). context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; return(context); }
private void DropExistingViews(string schema) { using (var context = new SchemaDbContext(_connectionString)) { foreach (var view in context.Views.Where(_ => _.TableSchema == schema).ToList()) { string sql = "DROP VIEW [" + view.TableSchema + "].[" + view.TableName + "];"; SqlUtil.ExecuteCommand(_connectionString, sql); } } }
private void CreateSchema(string schema) { using (var context = new SchemaDbContext(_connectionString)) { if (context.Schemata.SingleOrDefault(_ => _.SchemaName == schema) == null) { string sql = $"CREATE SCHEMA [{schema}] AUTHORIZATION [{_dataMartSchemaOwner}];"; SqlUtil.ExecuteCommand(_connectionString, sql); } } }
/// <summary> /// These tests are read only, but the do require a database. /// </summary> /// <param name="args"></param> static void Main(string[] args) { //change this connection string to a valid connection string to run your tests using (var db = new SchemaDbContext(@"Data Source=ZEKE-GPC-2016\SQLEXPRESS;Initial Catalog=AdventureWorks;uid=aw;pwd=1234;")) { Console.BufferHeight = 3000; Console.BufferWidth = 3000; var serviceProvider = db.GetInfrastructure <IServiceProvider>(); var loggerFactory = serviceProvider.GetService <ILoggerFactory>(); loggerFactory.AddProvider(new SqlLoggerProvider()); var dbType = db.GetType(); var dbProperties = dbType.GetProperties().OrderBy(pi => pi.Name).ToArray(); for (int i = 0; i < dbProperties.Length; i++) { Console.Clear(); var oe = dbProperties[i].GetValue(db); try { //if this fails move on to the next property var q = (IQueryable <object>)oe; var list = q.ToList(); Console.WriteLine(); if (list.Count > 0) { var et = list[0].GetType(); var p = et.GetProperties(); for (int j = 0; j < list.Count; j++) { for (int k = 0; k < p.Length; k++) { Console.Write($"{p[k].Name}: {p[k].GetValue(list[j])}"); if (k < p.Length - 1) { Console.Write(", "); } } Console.Write("\r\n"); } Console.WriteLine("\r\nPress enter to continue."); Console.ReadLine(); } else { Console.WriteLine("No data! Press enter to continue;"); Console.ReadLine(); } } catch (Exception) { //nothing to see here } } } }