I am in the process of migrating the database from SQL to postgres. The application code is using Entity framework 6. I am facing issues with case sensitivity as postgres stores table, column names in lower case unless using double quotes.
The data present in postgres is in lower case (table and column names). With Entity framework 6, I am facing issues with it not been able to find the table or column names as it is trying to use Pascal case.
I have searched and found few links:
Case insensitive name of tables and properties in Entity Framework 7
The above is for EF7 and most of the solutions doesn't work with EF6. I also tried to look for options in OnModelCreating but didn't find anything useful with DbModelBuilder as this is the object being passed to OnModelCreating.
I don't want to change all the entities to include the table and column names. I am wondering if one of you could help on this.
Appreciate any help.
Have a look at defining some custom Conventions or using the ModelBuilder to standardize case names. (see: https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/conventions/custom) The other options are to define table and column names via the attributes/configuraion.
Personally, with PostgreSQL I elected to use PascalCase for my table/column names. It meant my manual querying needed to use quotes, but frankly I'm more comfortable looking at naming conventions that way in databases.
Related
I have an Oracle database that has all its tables and columns in all Upper-Case.
For example table STUDENT has FIRSTNAME,LASTNAME and DATEOFBIRTH
when i generate classes using EF Database First approach, i get all classes and names in Upper-Case as well.
answer here
How to force pascal case with Oracle's Entity Framework support?
did not helped as it only generates names with only First letter in upper case instead of FirstName or LastName.
I thought of doing it manually. Is there a way i can write something in OnModelCreating so that every time i generate edmx i get the names right?
If i change name after generation its going to override next time i update it from database.
ReSharper may be able help with this, though I have not used Re# in a while or the naming convention feature. (see: https://www.jetbrains.com/help/resharper/Inspect_the_Whole_Solution_for_Naming_Style_Compliance.html) Tell it you want UpperCamelCase and run it across the generated code. There may be other plug-ins available with this capability.
EF isn't going to do it as I doubt they'd be trying to determine word boundaries for naming. howwillitknowhowmanywordsareinhere? :)
After having read a swag of SO questions about the Invalid Column Discriminator, I cannot see why I am running into the issue.
I have reverse-engineered a database Code-First using the EF Power Tools Visual Studio extension.
I have got an include path of "OrderRow.OrderRowOptions.Option" coming off my Orders DbSet. That is,
context.Orders.Include("OrderRow.OrderRowOptions.Option")
There is no Discriminator column on any of the tables and no Discriminator property in any of my POCOs.
The SQL which is being generated and sent to the database includes a Discriminator column.
Why? It's not like they are out of sync. I've repeated the reverse-engineering in a Console app just to make sure of that.
I believe I have finally figured out the problem here.
I always knew from the outset that it was related to EF's treatment of lookup tables.
And I knew that the Table Per Heirarchy ("TPH") feature was in play here.
The bit that I did not realize what that there does not actually need to be a Discriminator column.
I do have a TPH as there is a type called AdditionalLocationsOrderRowOption which inherits from OrderRowOption.
So, even though there is NO Discriminator column in either my database of my domain types (I've verified this countless times), the TPH is coming into play and preventing me from doing the Include on the Orders items.
So, my next step will be to use the Join method of EF-to-entities.
I have built a database using EF Codefirst (In an MVC4 application).
I've since learnt, that an external tool that will access this database has name-length limitations for columns & constraints.
Column names must be <=20 chars
Constraint names muse be <=10 chars
I'm therefor required to change the names of the generated DB constraints. (None of my column names exceed the limitation.)
Can I achieve this without destruction?
Does the migration framework provide the tools I need for such an operation?
As far as i know, Entity framework code first does not use the constraint name in its EntityTypeConfiguration. You should be able to rename the constraint to anything you like and just mention the relationships using the properties of your entities. This is how a configuration normally looks like.
// Relationships
this.HasRequired(t => t.Project)
.WithMany(t => t.ProjectInstances)
.HasForeignKey(d => d.ProjectId);
There is this handy little tool from entity framework team called Entity Framework Power Tools. You can generate the Code first entities and mappings by using that. You can use that tool in some test project and look at the generated code. I am pretty sure you will not have any issues even if you rename your constraints as long as you give the properties correctly.
I have been reading up on code first approach with entity framework. Unfortunately I can't find much documentation than what relates to EF4 on this. But the docs I have read (scott gu's blog on EF4) indicate that I don't need mappings.
So I generated a code files from an existing database using the EF6 Power Tools this generates all my model classes and a mappings folder. Automatically I looked at the mappings files in there which are using the Fluent API (I think this is correct) and describe details about the tables.
Now reading this makes sense that it possibly wouldn't know the Primary Key, Required Properties, Relationships but the thing I don't get is the Property to Column Mappings from the blog post these were not needed so why do I need them?
I can understand needing them if a column name can't be represented in code but my naming conventions don't allow this.
My main reason for asking is a maintainability question I would rather only have code for a particular property in one place and these lines this.Property(t => t.ID).HasColumnName("ID"); seem redundant to me.
Any one with any helpful links on EF6 code first approach would be appreciated as well google is failing :)
You certainly don't need property mappings if you're satisfied with the default column names and so on. You may need them for things like setting the order of columns in a compound primary key, or specifying that a property contains a database-generated value (like an identity/autoincrement column), but even then you can leave the column names out of it and stick with the defaults.
Column mappings do have some uses, but I'm not sure any of them are relevant to your situation:
You can map your entities to an existing database without having to mimic the column names, which may not follow standard .NET naming conventions.
Similarly, you can follow different naming conventions in your code vs. in your database. For example, where I work, database columns are usually expected to be camelCase, not PascalCase.
They allow you to change the names of your properties at a later date without having to recreate/migrate your database.
If none of those apply to you, then yeah, I think you're probably fine without them.
EF use conventions to do a lot. Once you know and feel comfortable with conventions you can declare classes and things just work.
Code first conventions
If we've been using an Entity Framework 4 model for some time, and we eventually want to switch the underlying database to a different vendor's product (say, from SQL Server to MySQL), is it simple to adjust the table and column mappings in the entity model without needing to update any of the entity class code?
We're trying to design code that is as database agnostic as possible, so I'd like to know in advance how much trouble we're in for if we ever switch our databases around. Ideally, we'd like to not have to touch our applications that use our entity classes. I can't seem to find any way in the entity designer or XML editor to adjust the underlying database column names without it giving me an error.
(I can, however, edit the entity's property names in the designer while leaving the database column names alone, but that's the opposite of what I need.)
Thanks!
EDMX is not database agnostic. SSDL part of EDMX is tightly coupled with database server (in case of MSSQL even with its version). You need separate SSDL for each supported database server.
I don't understand how changing column names relates to database agnostic model. Reverse is true! If you need your database to have different column names for different server products you need separate mapping for each of them!
Changing column names when using model first is possible only if you modify T4 template used for generating database creation SQL script. But every time you create that script designer will delete whole your storage description (SSDL) and mapping (MSL) and replace them with a new one.
The easiest way to have database agnostic code is using code first but even then you can have problems with some type and feature inconsistency among servers.
If you want database agnostic ORM you should probably check NHibernate.