简体   繁体   中英

change schema name at edmx file dynamically using entity framework 6 db first approach with Oracle DB

i have a UI page which contains, a drop down, with multiple values.

From a UI page, user will select one schema from the drop down, then the data related to that schema should be loaded to a grid. That means in future we may get more number of schema each with the same Oracle database and table structure.

Entity context already created using DB First approach with default config. but based on above requirement, I need to connect to Oracle DB based on Schema change.

While I use below didn't worked for me, It always point to the schema configured at connection string, not the schema that i'm sending to entity context.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            if (SchemaName != null)
            {
                modelBuilder.HasDefaultSchema(SchemaName);
            }
            base.OnModelCreating(modelBuilder);
            throw new UnintentionalCodeFirstException();
        }

can anybody suggest best way to do it?

I tried by applying schema name at model creation as above code This didn't work for me.

Actual need is, Entity context already created using DB First approach with default config. but based on above requirement, I need to connect to Oracle DB based on Schema change.

I found the solution by adding a helper class which will update the entity context files at run time by replacing the schema.

I just followed the url EF6 Dynamic Schema Change , which works well for me. I change the connection to use ORACLE, and called this 'connect' method from my service layer.

I suggest you create separate entity context for each Oracle schema. You can use the same Oracle account as long as that account has access to all the schemas (although I think it is way easier to use separate account for each schema). Depending on the schema selected at runtime, it is easy to use the correct entity context with an if-then-else statement. The schema for each entity class is embedded in the.edmx file, there's no worries that the query will fail even if using one Oracle account (provided access was granted).

Of course if you are only using one Oracle account then things gets complicated when creating the initial entity context. One approach is to use the original schema's account (or a temp account) then edit the app.config to the desired Oracle account afterwards or try this approach before removing the logon trigger once done (note: I didn't try this approach as I only tried the former).

Personally, I think having separate entity context for each schema with separate Oracle account is a cleaner and simpler approach rather than updating the entity context file dynamically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM