Entity Framework connect to mutiple database at runtime - asp.net-mvc-3

I want to ask if I had a proper design. Background:I develop my web application with EF4. The application will be used by three offices. The business processes in the three offices is similar with each other. Each office has the database their own, but most tables, SPROCs in the databases are the same.My thinking:I want to extract the same tables and stored procedure to a single edmx file, and point the connection string to database at runtime basing on the logon user.
I add a method to the constructor
MPREntities(string connectionString, string containerName)
And will pass the connectionString and the container name, when initializing the MPREntities. The containerName is the same - "MPREntities", it does not depend on the databases pointed to. The connectionString will be changed according to the logon user before passing in. I have done some testing and seems it works. But is that a normal approach? any suggestions?

That approach will work, and is a good use case for this.

Related

How can people collaborate in the same Oracle DB schema?

We are a team of tens of data analysts. Our main data back-end is an Oracle database. We use personal schemas to do work where we don't need to collaborate with others and we would like to create schemas dedicated to projects where people need to collaborate.
The problem is that in Oracle, one schema is equivalent to one DB user. If we create a schema dedicated to a project, for the purpose of creating DB objects in the context of that project, there will be a single set of credentials (username + password) that needs to be shared by all team members. This has two inconveniences:
if people mistype the credentials, they can block the account for everyone;
it is no longer possible to monitor who did what for security/audit reasons, since everyone uses the same schema;
An alternative would be that only one person uses the Schema user to create objects and assigns privileges to other people in those objects, but that can become quickly cumbersome.
Another alternative is to interact with the DB through R or Python but that means the credentials will be stored in some text file, which is bad for security.
As we see it, the ideal situation is if multiple personal DB users can create objects in the same schema, and if those objects are automatically available for that set of DB users. Is this totally impossible in Oracle? Is this impossible in any major DB? Is this requirement somehow flawed and as such, there is a good reason for why it is not available?
We could compare this collaboration in a DB schema to what commonly happens with people collaborating in a folder, using R, Python or other programming language for data analytics.
Thank you for your advise!
Maybe I miss something but could you not just create a schema that will be used for all users and grant the required privileges to each individual user?
Each user authenticates with his local account and by default uses his local schema and to access the public one you just use the ALTER SESSION SET CURRENT_SCHEMA command.

Public synonyms issue with two schemas on same instance

The database I am using is Oracle 11g Express Edition release 2.
I created 2 schemas in the same instance xe. They all have the same tables names and sequences names and stored procedures and stored functions and views names. But the tables structures and views texts are different ( there is some modifications between them ).
The reason for the creation of these two schemas is because our project has two versions. So the first schema is used for the first version , and the second schema was created for the second version. The mechanism of our web application Spring project is that whenever a connection is made through the web application login page then a corresponding Oracle user is making a connection according to the login entered ; so there is no fixed credential connection , there are Oracle users corresponding to each web application login.
So in order for each user to work with each database objects then I created public synonyms for every objects , and granted permissions to them for each user. But the database objects are owned by the schema I mentioned at the beginning. Now my problem is this : our customer wants the two project versions to be run on a same instance ( same computer server ). So one of the project version cannot run because the public synonyms can only refer to a particular schema owner. So how to make the public synonyms work for each schema ?
In short, you can't. However, you can always use a distinct synonym name to identify the object.
Something similar to below:
create public synonym structures_v1 for schema1.structures;
create public synonym structures_v2 for schema2.structures;
Oracle provides 2 totally different technologies for this situation (which comes to my mind):
Editions (and Edition Based Redefinition)
PDBs
With Editions you can create the same Object once in each Edition - but there are limitations like tables are not editionable.
It's not a feature you just enable, you need to understand the concept and implement it properly.
PDBs enable consolidation of Databases with colliding namespace (such as your described synonyms) within the same CDB and therefore save SGA/memory. Basically they are totally separated - limited interference can be implemented when it's concept of object & data inheritance is understand.
What about creating a 3rd Schema and having Synonym and permission to query 1st and 2nd schema. Anyone tested this concept?

How to connect to another DB with an already existing EF (4.1) DbContext?

I'm pretty new to EF and use to old school SqlConnection....
Question: I have an existing mvc3/EF database context object that already hits a local sql server 2008 instance. I want to add a new connection string in the web config and have the existing DBContext connect to a remote database to run a stored proc.
How can I do this?
If by existing context you mean the same context instance than it is not possible: one context instance = one connection string.
If you need to connect to two databases you need two context instances and pass connection string to them. Even in such case it can have many restrictions depending on your EF usage. Using the same context type for databases with different schema (different tables) doesn't always work as expected.
When using completely different databases the best way is to have two different context types and instance of each of them. But if you only want to execute stored procedure the simplest way is simply use ADO.NET SqlCommand and SqlConnection directly.
One of the ObjectContext constructors takes a connection string as a parameter. That should help.

EF ASP.net app connects to db even after connection strings are deleted in web.config?

Can somebody tell me how EF/asp.net caches db entries? I have a simple mvc3 project hitting a sqlserverexpress db. I want to change the setting in the webconfig to point to a new db but even if I change the connection strings or even delete the section the app still connects to the old database.
EF is a nice thing, but it will do some things without telling you.
You haven't given us much info, but if you're using EF Code First then your connectionstring name must match the name of the DB Context file. So if you have MoviesContext as the DB Context then you should have the same name for the connectionstring.
EF Code First uses a lot of conventions so if it doesn't find a connection string named as the DB Context it will try to connect to a SQLExpress (doesn't care if there is not one installed) and will look for/create the database with a very long name: Namespace.MovieContext.
Note: EF Code First might have gone off and created a database based on your POCO classes, if that is what you have done.
There is no caching involved in this case, it is just that EF Code First created a database for you...

Change Oracle Schema at runtime when using SubSonic

In my project, I am using Oracle Database and SubSonic for DAL. I have a problem with SubSonic and Oracle Schema, that is:
When developing, I used a schema DEV in Oracle Database and generate DAL using SubSonic.
After that when release to customer, he used a new schema TEST in Oracle Database and changed the connection string in app.config to connect to Oracle. The error will appear, that is “Table or View does not exist”. I found this error and see that the schema of tables is still DEV.
I do not want re-generate DAL after change schema and when released to the customer. Please help me.
Firstly, your schema should not be DEV. DEV is a user or role.
Your schema name should be related to the data content (eg ACCOUNTS or SALES)
Secondly, consider whether you or the customer is going to decide the schema name. Say you have a product called FLINTSTONE. You may decide that the schema name should be FLINTSTONE. However your customer may want to run two instances of your product (eg one for local sales, the other for international) and use the same database. So they want FS_LOCAL and FS_INTER as the schema names. Is that option a feature of your product ?
Next, decide if your application should connect as the schema owner. There are good security reasons for NOT doing that. For example, the schema owner has privileges to drop tables, which is generally something the application doesn't do and thus, on the principle of least privilege, is something your application shouldn't have privileges to do.
Generally I would recommend some config parameter for the application for the schema name, and after connecting to the database, the app should do an "ALTER SESSION SET CURRENT_SCHEMA = 'whatever was it the config file'". The application database user would need the appropriate insert/update/delete/select/execute privileges on the objects in the application schema. If the application can't do that, you can have a LOGON trigger in the database.
Gary is correct in not using DEV as a schema on your own machine. In using Oracle we typically set up the schema as what the client is going to name their schema. This however does not fix your issue. What you need to do is create a global alias in Oracle that maps say DEV to CLIENTSCHEMA. You should still rename the schema on your machine but this will allow your schema to differ from your clients.

Resources