How to know project is code-first or database-first?
Asked Answered
M

3

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.

Manny answered 11/5, 2017 at 14:29 Comment(3)
Any Migrations classes?Mckoy
@Mckoy did you mean player.cs class?Manny
No, I mean classes that has Migrations code (Inherit from DbMigration,I think)Mckoy
B
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).

Bakery answered 11/5, 2017 at 14:47 Comment(0)
C
1

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

Cyzicus answered 13/5, 2022 at 10:53 Comment(0)
S
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"

Stank answered 15/8, 2018 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.