Can I use Code First Migrations without pluarized table names? - ef-code-first

We finally got things squared away so that we can start using code first.
When I run "Enable-Migrations" it creates a file with InitialCreate in it's name.
(like here: http://msdn.microsoft.com/en-us/data/jj591621.aspx#enabling)
That file has plural database table names instead of singular.
I do have this line in my initiazlizer, which is correct:
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
but the file that is being created with the migration has all pluralized names.
This becomes annoying when I add a migration and the first thing it does is drop every pluralized table in the DB and re-add them as singular.
Can I pass a parameter into the "Enable Migrations" command or something to tell it to not pluralize?

This behavior has been fixed, I'm unable to reproduce it with EF 6.1.0. I created the database using an initializer first, than ran Enable-Migrations (of course OnModelCreating() already includes the line removing the PluralizingTableNameConvention). The generated migration (with InitialCreate in its name) generates tables named in the singular. So if you're able to update EF your problem will be gone.
If not: did you try to override the table names with their singular form using the Table attribute? At least generated migration code does respect it (I haven't checked with an older EF version than 6.1.0 though).
[Table("MyEntity")]
public class MyEntity
{
// .. your properties go here.
}

Related

Doctrine ORM -Remove attribute from entity?

Very simple thing I want to do, however I fail to find a solution.
I have started to develop my app with Symfony4 bundled with Doctrine. At the beginning I have designed database model but during development I have realized original solution is wrong way for what I want to do. Right now, I want to remove an attribute from entity. In plain SQL I would do ALTER TABLE table DROP COLUMN column then recreate it again with new parameters. However this solution gave me an error in Doctrine, so I have changed PHP model as well. Again, another error. Okay, so this does not look like the way I want to go.
Is there any solution to my problem, except digging too deep in Doctrine? Best would be something simple like described in the SQL command above?
When it comes to doctrine with symfony you should forget about tables, columns etc.. and start thinking with objects (otherwise there's no point in using doctrine for abstraction). For your problem you should simply remove the attribute in your entity.
Then You should use :
php bin/console doctrine:migrations:diff
Which will generate a migration file under the /migrations folder. This file will contain the SQL that doctrine will execute. It allows you to verify if the query is what you expect, if not you can directly modify it in that file or adapt your entity and generate a new migration file.
When you are happy with it, you can execute it with :
php bin/console doctrine:migrations:migrate

org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used

Iam inserting some data into DB with simpleJdbcInsert in spring , it works fine for first step (i mean for first insertion ) , when i try yo save the data for second time iam getting exception as :org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used."
Can any one help me out in this.
This exception usually happens when you try to config(again) a compiled simpleJdbcInsert.
compiled means you have instantiated a simpleJdbcInsert instance and set up data source and table name already.
Once an simpleJdbcInsert instance is compiled, you should not re-config it again; for instance, set another table name. Create a new simpleJdbcInsert instace if you need to do so.
To get a comprehensive understanding about how simpleJdbcInsert works, take a look into source code of simpleJdbcInsert and AbstractJdbcInsert. especially the method compile() in AbstractJdbcInsert.java

EF code first cant connect to the db

I followed this post step by step but when I'm running it and enter to the view I created I get:
Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
I dont know how to deal with it because I add Database.SetInitializer at the application_start and in the initializer I inherit DropCreateDatabaseIfModelChanges class, what am I missing?
Did you actually have the db created? Drop the db first to ensure its not there and let it create it and the proper model data should be initialized.

How to manually update Entity framework Code first model so that it updates a database (with a new column)?

can somebody tell me how I can add a data member (col) to my mvc3 model (class) and have it update the database without having to generate everything from scratch? I'm working from code first. When I change my model then run my project I get an error stating that model has changed. Any clean and easy way to synch creating a new col/data mamber with the db/model?
Thanks!
you can use this in application start,
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourDBContext>());
it will regenerate the database if your model change happens.And if you do not want to drop and create database (To incremental development) you can use SqlMigrations. http://www.hanselman.com/blog/EntityFrameworkCodeFirstMigrationsAlphaNuGetPackageOfTheWeek10.aspx

How to override record table naming to access existing data in Orchard CMS

I need to access data from pre-existing tables. I've started working my way through creating a module to display the data etc. However, Orchard is prefixing the table commands with the 'Table_Prefix' and 'Module Name'.
Is there any way I can specify what table to bind the model, so that I can use the existing IRepository
I'm trying to steer clear of modifying the core code, or implement my own IRepository ( which I've got a feeling is what I'm going to have to do.)
Thanks in advance.
You can create custom table naming convention (so that it would fit your current naming) by altering the core code, in three ways:
Record name mapping is created in BuildRecord method of
CompositionStrategy class
(Orchard.Framework/Environment/ShellBuilders/CompositionStrategy), so you can simply modify the code here.
By altering the Apply method of Orchard.Data.Conventions.RecordTableNameConvention class. This is where the record table name mappings (built in point 1.) get pushed to NHibernate.
Create your own implementation of FluentNHibernate.Conventions.IClassConvention (similar to RecordTableNameConvention mentioned above and replace the default one used by AutoMap in Orchard.Data.Providers.AbstractDataServicesProvider's CreatePersistenceModel(...) method with it.
You could also create your own IDataServicesProvider implementation, but that would surely be an overkill if you only need to change the table naming convention.
I was modifying CompositionStrategy and discovered that you have to modify the following
1. SetupService.cs (Modules\Orchard.Setup\Services):
Tables hardcoded in the Setup method are
"Orchard_Framework_DataMigrationRecord" and
"Settings_ShellDescriptorRecord"
2. InfosetController.cs (Modules\Upgrade\Controllers):
Multiple tables were hardcoded in this class which need to be updated.
3. DataMigrationManager.cs (Data\Migration):
Replace the SchemaBuilder parameters to the contructor.

Resources