Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Entity Framework Code First Default Naming Conventions

I'm trying to find a list of examples of Entity Framework's default naming conventions. I gather that Tables are <Entity>+s e.g. Users... Foreign Keys are User_Id But I would like to see a list of all out-of-the box conventions - IE what entity framework is going to call my objects.

I'm not looking for the types of the conventions - I got that. I am looking for definitions/examples of what Entity Framework's default conventions will name my database objects. To be clear, I have not added or removed any conventions. EF comes with a number of default conventions for generating databse objects - I want to know what EF's out-of-the box default conventions are going to name my database objects.

The reason I require this is because although I am using CodeFirst, I will be creating my database objects by hand rather than making EntityFramework generate it for me (my colleague is insisting on this). Therefore I need to know what I should name my Tables, Columns, Foreign Keys...... So that Entity Framework will find/map them with no explicit mappings required.

Eg

EF Default TableName Convention
      <Entity> + 's' (e.g ***Users***) 

EF Default Foreign Key Convention
      <Entity> + 'Id' (e.g ***UserId***)   

EF Default Primary Key Convention
      ***Id***  

Where can I find this?

like image 786
reach4thelasers Avatar asked May 09 '12 09:05

reach4thelasers


People also ask

What is the EF core convention for primary keys?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

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.

Which of the below are default code first convention to map or create a primary key column in the database?

By default, EF creates all the DB objects into the dbo schema. EF will create a DB table with the entity class name suffixed by 's' e.g. Student domain class (entity) would map to the Students table. EF will create a primary key column for the property named Id or <Entity Class Name> + "Id" (case insensitive).

Which of the following is the default convention to configure a Primarykey property in EF 6 code first?

Primary Key Convention Code First infers that a property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column.


2 Answers

EF does not provide a mechanism to retrieve the set of conventions used. You can only remove pre configured conventions.

You can find the documented convention list on MSDN

Initially EF allowed you to customize the conventions but now that part is removed from API.

Edit

Convention: Table Name
Default: Entity + s (e.g Users)

PluralizingTableNameConvention

Convention: Foreign Key Relation
Default: Entity + Id (e.g UserId)

NavigationPropertyNameForeignKeyDiscoveryConvention

Convention: Primary Key
Default: Id

IdKeyDiscoveryConvention

like image 104
Eranga Avatar answered Sep 28 '22 02:09

Eranga


Maybe the System.Data.Entity.ModelConfiguration.Conventions namespace is what you're looking for.
This namespace contains all naming conventions.

like image 42
Memo Avatar answered Sep 28 '22 00:09

Memo