I added this in my dbcontext. It is supposed to create the database but it doesnt.. what i am doing wrong ? Is'nt CreateDatabaseIfNotExists supposed to create the database once the context is created ?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProjectConfiguration());
Database.SetInitializer<PfsContext>(new CreateDatabaseIfNotExists<PfsContext>());
base.OnModelCreating(modelBuilder);
}
Database initializer will not create the database once you create the context. It should created the database once you use the context for date retrieval (execute query) or data persistence (save changes). You can also trigger creation manually:
context.Database.Initialize(false);
Related
I'm using Laravel 5 with Jajra/Datatables
The project use 3 databases, one is MySQL and 2 are commercial SQL databases.
The SQL databases have the tables with exactly same names.
If I want to display a table from MySql database I use in controller:
return Datatables::of(DB::table('coeficientVR_VanzariNoi')
->get(['id','marca','model','capacitate','combustibil', 'caroserie', 'altaClasaSchimb', 'coeficient',
]))->make(true);
and it's working great!
How to specify a table from one of the the SQL databases?
I have models associated to them, and models have the connection specified.
Example for one of table which is named "version":
class version_Jato extends Model
{
//
protected $connection = 'sqlJato';
protected $table = 'version';
protected $primaryKey = 'vehicle_id';
....
So I need to specify the SQL database but I don't know how.
Thank you for your time!
If you have already defined the $connection on each model you can query directly the model, i.e.:
return DataTables::eloquent(App\version_Jato::query())
->make();
You can read about it in the yajra/datatables docs.
Question
Are there any existing tools able to generate an implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider reflecting the state of an existing database?
I googled for a while and looked at similar questions in SO but i couldn't find an answer. They are all related to Symfony + Doctrine ORM.
Why do i need this?
I want to use doctrine migrations to manage/track changes on an existing database. I cannot jump into using doctrine ORM because I would need to refactor the database and it would break other ( non php ) applications that depend on it.
I know i can use migrations without ORM, but i need to provide a concrete implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider (documentation), in my case this would mean to rewrite the entire database.
If i had the SchemaProvider generated for the first time to reflect the state of the database. Eg
<?php
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\Provider\StubSchemaProvider;
final class MySchemaProvider implements SchemaProvider {
public function createSchema()
{
$schema = new Schema();
$table = $schema->createTable('foo');
$table->addColumn('id', 'integer', array(
'autoincrement' => true,
));
$table->setPrimaryKey(array('id'));
//and so on for the rest of the databse...
return $schema;
}
}
I would be able to edit the generated class and create migrations with:
$ ./doctrine migrations:diff
I have to generate JPA objects for mysql tables using odata4J , as i am new to web services i have followed some online resources created following tasks;
Created new database connection under "Database Development"
Added eclipse link and mysql connector libraries through windows-> preferences->user library
Changed project Facets to JPA and configured further JPA configurations
Persistence.xml is generated and Populated Transaction Type from "Resource Local" under connection tab in the Persistence.xml
Generated JPA Entities from table by Right clicking on Project and selecting JPA Tools -> Generate entities from Table option.
One Java file is generated with the name of Table name.
I have written one Java class called TestProducer
public class TestProducer implements ODataProducerFactory {
String persistence = "testJPA";
String namespace = "testui5";
int maxResults = 50;
#Override
public ODataProducer create(Properties properties) {
String endpointUri = "http://localhost:8080/TestProducer.svc/";
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistence);
JPAProducer producer = new JPAProducer(emf, namespace, maxResults);
DefaultODataProducerProvider.setInstance(producer);
return producer;
}
}
Am able to get result on console by calling create method in TestProducer from Main
But can any one tell me step by step procedure to access JPA Object through http , am not getting any online resouces.Please help me ..thanks in advance.
Is there a way to recreate an EntityManagerFactory already used in a webapplication at runtime? What I want is tell the entityManagerFactory to forget the last database connection and connect to a new schema at runtime when the webuser selects an other database (mysql schema). Or not forget the already used one but also use a new connection to a different mysql schema which was not used yet. These schemas have the exact same structure (tables, etc.) but for different users for security and other reasons. Is this possible?
I am using the Vaadin framework, Eclipselink 2.4.1, Mysql 5.5 and Tomcat 7. What I found related to my situation and what I tried already are followings. I am using an Eclipselink composite persistence unit, the second member is always the same, I want to change the first schema for example to "42_candidate" when the user selects the 42nd candidate on the webpage after an other schema was already connected like "41_candidate".
private static EntityManager createEntityManagerForCandidateSchema(String candidateSchemaName) throws javax.persistence.PersistenceException {
System.out.println("createEntityManagerForCandidateSchema called");
// set persistence unit properties
HashMap<String,String> candidatePuProps = new HashMap<String,String>();
candidatePuProps.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/"+candidateSchemaName+"?useUnicode=true&characterEncoding=UTF-8");
HashMap<String,Map> compositePuProps = new HashMap<String,Map>();
compositePuProps.put("election_model_candidate", candidatePuProps);
Map puProps = new HashMap();
puProps.put("eclipselink.logging.level", "FINEST");
puProps.put("eclipselink.composite-unit.properties", compositePuProps);
// puProps.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, "com.beharbe.ui.ElectionSessionCustomizer");
boolean candidateDatabaseSchemaNotFound = false;
try {
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("election_composite_pu",puProps);
emf.close(); // to forget latest things
emf = javax.persistence.Persistence.createEntityManagerFactory("election_composite_pu",puProps);
EntityManager em = emf.createEntityManager(compositePuProps);
...
public void selectionChanged(int newCandidatePersonId) {
entityManager = Util.createEntityManagerForCandidateSchema(newCandidatePersonId);
...
(Eclipselink Composite PU)
wiki.eclipse.org/EclipseLink/UserGuide/sandbox/gelernter/Composite_Persistence_Units#Persistence_Unit_Properties
(Dynamic Persistence)
dev.eclipse.org/svnroot/rt/org.eclipse.persistence/branches/2.1/trunk/examples/jpa.employee/eclipselink.example.jpa.employee.dynamic/src/example/Main.java
(EclipseLink - How to configure Database Schema name at runtime)
www.rqna.net/qna/kxvmwy-jpa-eclipselink-how-to-configure-database-schema-name-at-runtime.html
(Eclipselink with Tomcat tutorial)
wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial#Session_Customizer
(Using JPAContainer with Hibernate (Vaadin))
vaadin.com/book/-/page/jpacontainer.hibernate.html
(How can I make a JPA application access different databases?)
stackoverflow.com/questions/9315593/how-can-i-make-a-jpa-application-access-different-databases
(Connect two or more databases dynamically)
stackoverflow.com/questions/9732750/connect-two-or-more-databases-dynamically?lq=1
(JPA - Using Multiple data sources to define access control)
www.rqna.net/qna/kqqihk-jpa-using-multiple-data-sources-to-define-access-control.html
JPA2 run-time database connection
JPA - EclipseLink - How to configure Database Schema name at runtime (Eclipselink SessionCustomizer)
Maybe I should do it somehow with the EclipseLink SessionCustomizer? (see latest link)
thanks for any help in advance
Meanwhile I found something, this can be what I have to use:
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing
I am trying this way but it still connects to the schema which was connected first when the EMF called first time:
....
candidatePuProps.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://localhost:3306/"+candidateSchemaName+"?useUnicode=true&characterEncoding=UTF-8");
candidatePuProps.put(PersistenceUnitProperties.EXCLUSIVE_CONNECTION_MODE, "Always");
candidatePuProps.put(PersistenceUnitProperties.EXCLUSIVE_CONNECTION_IS_LAZY, "false");
...
If you want to access two different databases/schemas, you can just call createEntityManagerFactory() passing a properties map with your new connection.
To set the schema with EclipseLink in code you can set the tableQualifier in a Customizer. To get a new EntityManagerFactory you can pass the "eclipselink.session-name" property.
Your code looks correct. What version of EclipseLink are you using?
Remove the composite persistence unit, I think you seem to just want to connect to a different database. Composite persistence units are for when you have relationships across databases.
With linq to sql, where is the database connection information stored?
How could I override the database to another database on a per query basis?
You can't do it per-query; but you can per-data-context. Just pass in a different connection or connection-string to the constructor:
string connectionStringA = ..., connectionStringB = ...
using(var ctxA = new FooContext(connectionStringA)) {...}
...
using(var ctxB = new FooContext(connectionStringB)) {...}
using(SqlConnection conn = ...)
using(var ctxC = new FooContext(conn)) {...}
The database connection for linq is in the web.config or application.config files.
You pass it to the DataContext instance.
With a generated DataContext subclass, the default constructor will use project .Settings, and thus from the .config file.
As aleemb said, the database information is stored in the config files. Check the one in the project where you created your dbml map.
That said - the DatabaseContext has a constructor that takes the connection string as a parameter. However, I'm not sure there's a good way to override that on a per query basis without creating a new DatabaseContext object. Which really could cause you issues in the future if you're creating entities from two different DatabaseContext objects.