Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Add this constructor on every DbContext that you want to change the schema on runtime
- public MyDbContext(DbConnection connection) : base(connection,true) {}
- //Create a new DbContext of type <T> using the connection string and schema set by you
- public static T Connect<T>(string connectionString, string schema) where T : DbContext
- {
- var assembly = typeof(T).Assembly;
- var type = typeof(T);
- var resourceNames = assembly.GetManifestResourceNames();
- string contextName;
- // code used to avoid of getting the wrong csdl, used when you have more than one
- // DbContext on your project
- foreach (var csdl in resourceNames.Where(r => r.EndsWith(".csdl")))
- {
- var csdlDocument = XDocument.Load(assembly.GetManifestResourceStream(csdl));
- XNamespace csdlNamespace = "http://schemas.microsoft.com/ado/2008/09/edm";
- var name = csdlDocument.Root.Elements(csdlNamespace + "EntityContainer").First().Attribute("Name").Value;
- if (type.Name.Equals(name))
- {
- contextName = csdl.Replace(".csdl", "");
- break;
- }
- }
- string csdlName = resourceNames.Single(r => r.Equals(contextName + ".csdl"));
- string ssdlName = resourceNames.Single(r => r.Equals(contextName + ".ssdl"));
- string mslName = resourceNames.Single(r => r.Equals(contextName + ".msl"));
- var ssdlDocument = XDocument.Load(assembly.GetManifestResourceStream(ssdlName));
- XNamespace ssdlNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/ssdl";
- var functions = ssdlDocument.Root.Elements(ssdlNamespace + "Function").ToList();
- foreach (var f in functions)
- f.SetAttributeValue("Schema", schema);
- var entitySets = ssdlDocument.Root.Elements(ssdlNamespace + "EntityContainer").ToList().Elements(ssdlNamespace + "EntitySet").ToList();
- foreach (var es in entitySets)
- if (es.Attribute("Schema") != null) es.SetAttributeValue("Schema", schema);
- Func<string, XmlReader[]> getFromResource = (name) =>
- {
- using (var s = assembly.GetManifestResourceStream(name))
- {
- return new XmlReader[] { XDocument.Load(s).CreateReader() };
- }
- };
- var storeItems = new StoreItemCollection(new XmlReader[] { ssdlDocument.CreateReader() });
- var edmItems = new EdmItemCollection(getFromResource(csdlName));
- var mappingItems = new StorageMappingItemCollection(edmItems, storeItems, getFromResource(mslName));
- var workspace = new MetadataWorkspace();
- workspace.RegisterItemCollection(storeItems);
- workspace.RegisterItemCollection(edmItems);
- workspace.RegisterItemCollection(mappingItems);
- workspace.RegisterItemCollection(new ObjectItemCollection());
- workspace.LoadFromAssembly(assembly);
- // I´m using Oracle, so i need to change the connection
- // if you´re using SQL Server use SqlConnection
- var storeConn = new Oracle.DataAccess.Client.OracleConnection(connectionString);
- ConstructorInfo contextConstructor = typeof(T).GetConstructor(new Type[] { typeof(DbConnection) });
- var entityConn = new EntityConnection(workspace, storeConn);
- return (T)contextConstructor.Invoke(new Object[] { entityConn });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement