In this article, we’ll learn how to create a foreign key in the code first approach.

If you are new to the code first approach, you can refer to this article which covers CRUD operations with a code-first approach in an MVC application.

Here, we are going to create a foreign key in the existing table (MyFirstTables) using migration.

Note: If MyFirstTables is not already existed then it will be created automatically.

Let’s start.

Firstly open the MyFirstTable.cs file and add the code in it.

using System;  
using System.Collections.Generic;  
using System.ComponentModel.DataAnnotations;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
 
namespace Code_First_Demo.Models  
{  
    public class MyFirstTable  
    {  
        [Key]  
        public int ID { get; set; }  
        public string Name { get; set; }  
        public int Age { get; set; }
        public int CID { get; set; }
        public CountryTable Country { get; set; }
    }
    public class CountryTable
    {  
        [Key]  
        public int CID { get; set; }  
        public string CName { get; set; }
    }
}

Now, open the CDBContext.cs  file and add the code in it.

 
using Code_First_Demo.Models;  
using System;  
using System.Collections.Generic;  
using System.Data.Entity;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;
 
namespace Code_First_Demo.Repository  
{  
    public class CDBContext : DbContext  
    {  
        public CDBContext() : base("StringDBContext")  
        {  
        }  
        public DbSet<MyFirstTable> MyFirstTable { get; set; }
        public DbSet<CountryTable> CountryTable { get; set; }
    }  
}

Now, open the package manager console to fire the commands.

  • Add-Migration MigrationName

Note: You can put any migration name you choose.

Add-Migration AddForeignKey
  • Update-Database

Note: This command will Create/modify your database.

Update-Database

The above example depicts a property name CID in MyFirstTable entity matches with the primary key property of CountryTable entity, so CID in MyFirstTable entity will automatically become a foreign key property, as shown below.

For a foreign key, the [ForeignKey] attribute overrides the default convention. It allows us to specify in the dependent entity the foreign key property whose name does not match with the primary key property of the principal entity.

The [ForeignKey] attribute on the foreign key property in the dependent entity and the related navigation property name can be specified as a parameter as shown in the example below.

Now just open the MyFirstTable.cs file and replace the code in it.

using System;  
using System.Collections.Generic;  
using System.ComponentModel.DataAnnotations;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.ComponentModel.DataAnnotations.Schema;
 
namespace Code_First_Demo.Models  
{  
    public class MyFirstTable  
    {  
        [Key]  
        public int ID { get; set; }  
        public string Name { get; set; }  
        public int Age { get; set; }

        [ForeignKey("Country")]
        public int CID { get; set; }
        public CountryTable Country { get; set; }
    }
    public class CountryTable
    {  
        [Key]  
        public int ID { get; set; }  
        public string CName { get; set; }
    }
}

As we can see CountryTable has a primary key, named ID. Where MyFirstTable has a foreign key, named CID.

That’s it.

 

Also, check How To Use FOREIGN KEY Constraint In SQL

Subscribe

Select Categories