8

In an existing project, how do I know if it's code-first or database-first?

Project has this lines of code:

public class TestDBContext : DbContext
{
    public DbSet<Player> Players { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

And project has no .edmx file. If any other details need I will share.

EDIT:

Player.cs class

public class Player
{
    public int PlayerID { get; set; }
    public string PlayerName { get; set; }
}

EDIT 12.05.2017

IF I change database name from connection string and run project, it creates database with the new name with all tables. May be this will be hit for the answer.

kgzdev
  • 2,770
  • 2
  • 18
  • 35

3 Answers3

8

If this is a project is Database-first, there is :


  • [name].edmx diagram file and with it, [name].Context.tt & .cs
  • every tables that are translated into class are hidden in tree like .edmx > .tt
  • in OnModelCreating, there is a throw new UnintentionalCodeFirstException()

If not, all the class issue from the tables are in the project (no tree).

SharpC
  • 6,974
  • 4
  • 45
  • 40
Totolouis
  • 96
  • 3
1

If there is no .edmx file, the project is code-first.

Adok
  • 49
  • 7
0

Here I am sharing my observation.

Mainly there are two approaches to implement Entity Framework. 1. Code-first If chosen, it will create simple .cs file(s) which developers later modifies as per their requirement.

  1. Data-first If chosen, it will create a [name].edmx file along with hierarchy of different files. It contains .Context.tt and underneath .Context.cs file. The .Context.cs file will have below snippet which indicates whether induced entity model was empty when created or it was with any database object.

    namespace Search { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure;

    public partial class XYZ_MSCRMEntities : DbContext
    {
       public XYZ_MSCRMEntities()
        : base("name=xyz_MSCRMEntities")
       {
    }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    
    public virtual DbSet<AnyDatabaseTableOrView> TableOrViewPluralized { get; set; }
    }}
    

In above snippet, very last line (DbSet property) shows that it has imported database object and that is how it is "Data-first"

Binoy
  • 390
  • 5
  • 19