MVC 3 CodeFirst Share sql express database with all of my models - database

I am creating a data driven website with mvc3 using EF and code first. I am trying to have all of my models share one database so that seperate roles can have access to common tables. I am not sure weather to just use one db context because that seems like it might be the wrong way. I do not want my models to create their own seperate databases. I want them to share but I cant seem to achieve this. Thank to any help.

Instantiating multiple DbContexts will not cause you to have multiple databases unless you pass different connection strings into each one. If you have a single connection string, which will typically be kept in your web.config file, you can have many DbContexts, but they will all share the same database.

You must deploy your database directly to SQL Server instead of using local file in AppData => your connection string mustn't use AttachDBFile.

I was setting seperate DB contexts in all of my Models. I know, what a noob mistake. Thanks for the quick responses anyhow. THIS helped me figure it out and its really and amazaing resource for this stuff

Related

Is it possible to have separate DB for each areas in .net-core along with project's DB?

I need to create separate database for area in a project. I don't want to join two database together. is it possible or not ?
Actually I wanted to have totally separated projects in one solution but this way I have some issue with routes (only one of them is accessible by routes) so i decided to try area but still I need to have more than one DBs in a project
Yes, of course.
I do not know how you connect to your database, but in your appsettings.json file, put two connection strings with different names for different DB's.

Yii2 Giving ActiveRecord Model Database Details on Construction

I am construction a login system for Yii2 that can create the identity from either within a table in the Yii2 application or from data from an external database, the data could reside in one of many, many databases.
I know i could setup second, third, fourth databases in the apps config, but it doesn't fit the use case as, as the database belongs to a user who could change it, etc.
What i need to do it instantiate an ActiveRecord model passing it the database details of the database it should connect to, to query.
I have managed to very much confuse myself over this, in trying to work out the correct way to do it.
I know you can pass a config array to the constructor, but am unable to understand how i should do it, should i create and object of \yii\db\connection and pass it to the __construct?
If so how do i then perform the connection ?
I have searched all over for similar use case, but am unable to discover anything that helps, even a pointer would be great.
Many Thanks
Ok, so after so more head banging and searching in a different way, i have discovered a very simple and sensible approach, and the answer does come from SO.
Yii2 set db connection at runtime

How to read data from SQLite database on Eclipse

I'm using a static database that I created with SQLite Database Browser. I put it in my assets folder and built a code to copy the database to a database variable (Does that make sense?) so I could read information from it. Problem is I don't know how - mostly the SQL queries involved - and what are your suggested methods do to that? In other words, what methods should I add to my Database Handler class (Or data adapter?) in order to present the data in a list view, for example.
Thank you for all your help.
Read the Android database documentation.
Copying your database from the assets folder is typically done in the onCreate and/or onUpgrade functions of your SQLiteOpenHelper-derived class.
This tutorial covers the basics:
Using the SQLite Database with ListView
As for naming things: use whatever names make sense in your application.

Symfony 2: Build own DB access layer

I'm new here and on a research trip. We would like to use Symfony2 for a new project.
Now we have the problem that we need to use a company wide self-developed DB access layer.
We don't want to lose the whole Symfony/Doctrine layer for handling data.
Our idea is now to create or overwrite database access layer below Doctrine.
Something like creating a bridge (like PDO) between Doctrine and our DB access layer.
Has anyone an idea how we can do this or maybe someone has done similar things already and can provide us with an how-to?
Please no questions why. We just have to use the company access layer!
Thank you
http://forum.symfony-project.org/viewtopic.php?f=23&t=37637
I found the solution in Doctrine2 doc. It's pretty easy.
I needed for development and testing (frontend and console) about half a day
I've done it in 4 steps:
copy Doctrine\DBAL\Driver\OCI8 to a new folder.
rename files.
change functions to use own database access layer.
add in config file
doctrine:
dbal:
driver_class: /Path/To/Driver/Class
Doctrine has not much but a bit of information about Abstraction Layer:
Supporting Other Databases
If you believe this ยง of the doctrine documentation, you can see that you may use the Common package because it does not require the DBAL. Sadly, the same thing cannot be said of the ORM package. The best thing to do is probably to fork this package so that it supports your own DBAL.
Condolences for being obliged to use this DBAL.

Is there any way to access a datasource from one project, in another project?

I have access to a database table that has been added as a datasource in project (A). Is there any way to refer to this database table from project (B) WITHOUT adding it to project (B)'s datasource?
i.e.
ProjB.databasetable.GetDataSetSchema
Any ideas would be really appreciated. Thanks guys!
The database table is not related to the project A in any way. The project A contains a datasource, probably a DataSet, that is used to access the table.
If you would like to reuse that dataset, just add a reference to ProjectA to ProjectB and ProjectA.MyDataSet will be there for you to use.
The way is to use remote data server and all application will connect to your server.
You can make this with DataAbstract
but you need to rewrite your application.

Resources