How to know project is code-first or database-first?

十年热恋 提交于 2020-08-27 07:38:40

问题


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.


回答1:


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).




回答2:


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"



来源:https://stackoverflow.com/questions/43918332/how-to-know-project-is-code-first-or-database-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!