1) Entity Framework Code First pluralizes the tables names. For example: if the class name is Student then Code First creates a table with name Students.
Code First uses Entity Framework's Pluralization Service to pluralize the table names.
If you want to remove the table naming convention, you can do that by overriding the OnModelCreating() method like follows:
3) The column names are same as the class property names. For example: if there is a property named FirstName in the Student class then Code First creates a column with name FirstName in the Students table.
4) Any property of the class with name "Id"(case insensitive) or {class name} + "Id" will be treated as primary key in the table. In the Student.cs class file if there is a property with name Id or StudentId then it will be treated as the primary key in the Students table.
5) For string properties, by default, Code First creates a Nullable column with data type nvarchar(max) in MS SQL Server. For example, if the Student.cs class file has a property FirstName which is of type string then Code First create a column named FirstName(null, nvarchar(max)) in the Students table.
6) One-to-many relationship convention: Let's assume we have two entities Student and Standard and the supporting classes are Student.cs and Standard.cs. There is one-to-many relation between Standard and Student. So one Standard contains many Students and one student can belongs to only one standard.
Convention for creating the foreign key is : <Navigation Property Name>_<Primary key of the related class>
Example:
The generated columns are:
The generated SQL(for MS SQL Server) for Students table is:
The generated SQL for Standards table is:
Code First uses Entity Framework's Pluralization Service to pluralize the table names.
If you want to remove the table naming convention, you can do that by overriding the OnModelCreating() method like follows:
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
2) By default each table is created with dbo schema.3) The column names are same as the class property names. For example: if there is a property named FirstName in the Student class then Code First creates a column with name FirstName in the Students table.
4) Any property of the class with name "Id"(case insensitive) or {class name} + "Id" will be treated as primary key in the table. In the Student.cs class file if there is a property with name Id or StudentId then it will be treated as the primary key in the Students table.
5) For string properties, by default, Code First creates a Nullable column with data type nvarchar(max) in MS SQL Server. For example, if the Student.cs class file has a property FirstName which is of type string then Code First create a column named FirstName(null, nvarchar(max)) in the Students table.
6) One-to-many relationship convention: Let's assume we have two entities Student and Standard and the supporting classes are Student.cs and Standard.cs. There is one-to-many relation between Standard and Student. So one Standard contains many Students and one student can belongs to only one standard.
Convention for creating the foreign key is : <Navigation Property Name>_<Primary key of the related class>
Example:
public class Student { public int Id { get; set; } public string Name { get; set; } public virtual Standard Standard { get; set; } }
Here note that we don't have any foreign key column in the Student class. So Code First creates a foreign key column with name = {NavigationProperty Name}_{Primay key property name} i.e Standard_StandardId in Students table.public class Standard { public Standard() { Students = new List<Student>(); } public int StandardId { get; set; } public string Name { get; set; } public ICollection<Student> Students { get; set; } }
The generated columns are:
The generated SQL(for MS SQL Server) for Students table is:
CREATE TABLE [dbo].[Students] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (MAX) NULL, [Standard_StandardId] INT NULL, CONSTRAINT [PK_dbo.Students] PRIMARY KEY CLUSTERED ([Id] ASC), CONSTRAINT [FK_dbo.Students_dbo.Standards_Standard_StandardId] FOREIGN KEY ([Standard_StandardId]) REFERENCES [dbo].[Standards] ([StandardId]) ); GO CREATE NONCLUSTERED INDEX [IX_Standard_StandardId] ON [dbo].[Students]([Standard_StandardId] ASC);
CREATE TABLE [dbo].[Standards] ( [StandardId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (MAX) NULL, CONSTRAINT [PK_dbo.Standards] PRIMARY KEY CLUSTERED ([StandardId] ASC) );
