Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4 Conventions

Is it possible in EF4 to change conventions for a whole DbContext rather than on a per entity basis?

Currently when I run EF4 queries I'm getting an error on foreign key relationships

Invalid column name 'Account_Id'.

The table being queried (User) has a column named AccountId which is a foreign key to the related table (Account), if I change the column to Account_Id, it solves the problem.

However, the issue is that I don't want to do this on every table. I've set custom conventions no problem using NHibernate, so hopefully its as simple in EF4.

like image 511
William Avatar asked Oct 02 '10 14:10

William


People also ask

What is conventions in Entity Framework?

Conventions are a set of rules hard-baked into Entity Framework Core that govern how the model will be mapped to a database schema. Most of the time, especially with new application development, it makes sense to follow EF Core's conventions when developing the model.

What are different types of Entity Framework approaches?

There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.

What is the EF core convention for primary keys?

EF Core will create the primary key column for the property named Id or <Entity Class Name>Id (case insensitive). For example, EF Core will create a column as PrimaryKey in the Students table if the Student class includes a property named id, ID, iD, Id, studentid, StudentId, STUDENTID, or sTUdentID.

What is difference between Entity Framework 5 and 6?

EF5 is built into the core of . NET 4.5, whereas EF6 has been shifted out, and is open source. This means that you must add the new EF6 assemblies to all of the relevant projects in the solution, in particular the entry project. This means that you must remove assembly System.


1 Answers

I don't think there is support for global settings. You can use fluent API, as you probably know, but this works for single entity only. I've decided to keep EF4 conventions and use xxxID for primary keys and table1_table2 for linker tables. It's easier to swim with, not against the current ;-). You might want to fire reflector to check the code for an easy way to hack it...

like image 187
Jakub Konecki Avatar answered Oct 06 '22 00:10

Jakub Konecki