/// <summary> /// </summary> /// <param name="_SubmittedBaseDoc"></param> /// <returns>Something that Entity Framework Code First would contract the same SQL objects read to create this type</returns> private static Type ReverseEngineerCodeFirst(BaseDoc _SubmittedBaseDoc) { if (!ReverseEngineerCodeFirstDic.ContainsKey(_SubmittedBaseDoc.DocTypeName)) { string sqlAsCharp = string.Empty; try { // attempt to observe any tables that would accommodate this DocTypeName in the database now as this will be needed the remaining execution of this appdomain to assess code first auto-migrations // in the database itself, the namespace of a given document has no baring on how the doc it persisted in sql or the structure it may spawn sqlAsCharp = Handler.ReverseEngineerCodeFirst( RuntimeTypeNamer.CalcCSharpFullname(_SubmittedBaseDoc.DocTypeName, "0.0.0.0", "SqlToNonBaseDoc"), _SubmittedBaseDoc.DocTypeName, SqlDbContext.GetConnectionString()); } catch (Exception) { /*TODO:Check for database instead of letting this fail*/ } ReverseEngineerCodeFirstDic[_SubmittedBaseDoc.DocTypeName] = ClassMerger.Instance.MergeOnPropertyNames( RuntimeTypeNamer.CalcCSharpFullname(_SubmittedBaseDoc.DocTypeName, "0.0.0.0"), new[] { _SubmittedBaseDoc.GetType(), string.IsNullOrWhiteSpace(sqlAsCharp) ? _SubmittedBaseDoc.GetType() : Runtime.CompileCSharpCode( sqlAsCharp, _SubmittedBaseDoc.DocTypeName, "0.0.0.0", "PostMergeTo", _SubmittedBaseDoc.solutionVersion).GetTypes().First(t => t.Name == _SubmittedBaseDoc.DocTypeName) }, _SubmittedBaseDoc.DocTypeName, new[] { "Any", "Xml_Element" }, // ignore XmlElement Any properties true, typeof(BaseDoc)); } return(ReverseEngineerCodeFirstDic[_SubmittedBaseDoc.DocTypeName]); }
/// <summary> /// establishes one DBContext & sql schema per-csharp-namespace /// </summary> /// <param name="_BaseDoc"></param> private SqlDB(BaseDoc _BaseDoc) : base(false) { lock (InitializingDocTypeNameLock) { InitializingDocTypeName = _BaseDoc.DocTypeName; _underlyingDbContext = SqlDbContext.CreateInstance(_BaseDoc); if (!_underlyingDbContext.Database.Exists()) { _underlyingDbContext.Database.Create(); } _underlyingDbContext.Database.CompatibleWithModel(false); // register with meta data model so we can use it's features later RegisterContext( new EFDataModelProvider(() => _underlyingDbContext), new ContextConfiguration { ScaffoldAllTables = true } ); } }