Advertisement
Guest User

Oracle and Entity Framework 5 Schema Redirection

a guest
Mar 7th, 2014
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. //Add this constructor on every DbContext that you want to change the schema on runtime
  2. public MyDbContext(DbConnection connection)  : base(connection,true) {}
  3.  
  4. //Create a new DbContext of type <T> using the connection string and schema set by you
  5. public static T Connect<T>(string connectionString, string schema) where T : DbContext
  6. {
  7.             var assembly = typeof(T).Assembly;
  8.  
  9.             var type = typeof(T);
  10.             var resourceNames = assembly.GetManifestResourceNames();
  11.             string contextName;
  12.    
  13.         // code used to avoid of getting the wrong csdl, used when you have more than one
  14.         // DbContext on your project
  15.             foreach (var csdl in resourceNames.Where(r => r.EndsWith(".csdl")))
  16.             {
  17.                 var csdlDocument = XDocument.Load(assembly.GetManifestResourceStream(csdl));
  18.                 XNamespace csdlNamespace = "http://schemas.microsoft.com/ado/2008/09/edm";
  19.                 var name = csdlDocument.Root.Elements(csdlNamespace + "EntityContainer").First().Attribute("Name").Value;
  20.  
  21.                 if (type.Name.Equals(name))
  22.                 {
  23.                     contextName = csdl.Replace(".csdl", "");
  24.                     break;
  25.                 }
  26.             }
  27.  
  28.             string csdlName = resourceNames.Single(r => r.Equals(contextName + ".csdl"));
  29.             string ssdlName = resourceNames.Single(r => r.Equals(contextName + ".ssdl"));
  30.             string mslName = resourceNames.Single(r => r.Equals(contextName + ".msl"));
  31.  
  32.             var ssdlDocument = XDocument.Load(assembly.GetManifestResourceStream(ssdlName));
  33.             XNamespace ssdlNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/ssdl";
  34.             var functions = ssdlDocument.Root.Elements(ssdlNamespace + "Function").ToList();
  35.  
  36.             foreach (var f in functions)            
  37.                 f.SetAttributeValue("Schema", schema);
  38.            
  39.             var entitySets = ssdlDocument.Root.Elements(ssdlNamespace + "EntityContainer").ToList().Elements(ssdlNamespace + "EntitySet").ToList();
  40.  
  41.             foreach (var es in entitySets)            
  42.                 if (es.Attribute("Schema") != null) es.SetAttributeValue("Schema", schema);            
  43.  
  44.             Func<string, XmlReader[]> getFromResource = (name) =>
  45.             {
  46.                 using (var s = assembly.GetManifestResourceStream(name))
  47.                 {
  48.                     return new XmlReader[] { XDocument.Load(s).CreateReader() };
  49.                 }
  50.             };
  51.  
  52.             var storeItems = new StoreItemCollection(new XmlReader[] { ssdlDocument.CreateReader() });
  53.             var edmItems = new EdmItemCollection(getFromResource(csdlName));
  54.             var mappingItems = new StorageMappingItemCollection(edmItems, storeItems, getFromResource(mslName));
  55.  
  56.             var workspace = new MetadataWorkspace();
  57.             workspace.RegisterItemCollection(storeItems);
  58.             workspace.RegisterItemCollection(edmItems);
  59.             workspace.RegisterItemCollection(mappingItems);
  60.             workspace.RegisterItemCollection(new ObjectItemCollection());
  61.             workspace.LoadFromAssembly(assembly);
  62.        
  63.         // I´m using Oracle, so i need to change the connection
  64.             // if you´re using SQL Server use SqlConnection
  65.             var storeConn = new Oracle.DataAccess.Client.OracleConnection(connectionString);
  66.  
  67.             ConstructorInfo contextConstructor = typeof(T).GetConstructor(new Type[] { typeof(DbConnection) });
  68.  
  69.             var entityConn = new EntityConnection(workspace, storeConn);
  70.  
  71.             return (T)contextConstructor.Invoke(new Object[] { entityConn });
  72.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement