Entity Framework CodeFirst : Where is my database

Entity Framework CodeFirst : Where is my database

  • Comments 1

Sometime back I was trying out on CodeFirst and I suddenly realized that the generated database by CodeFirst is missing. I had to see in SSMS what exactly got generated. So I did a trick to retrieve the database information

//Employee Entity
public class Employee
{
    public int Id { get; set; }
    public string FullName { get; set; }
}
public class HRContext : DbContext
{
    public DbSet<Employee> Emps { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var emp = new Employee() { FullName = "Wriju Ghosh" };
        using (var ctx = new HRContext())
        {
            //This would get me the database information
            string strConnection = ctx.Database.Connection.ConnectionString;
            Console.WriteLine(strConnection);
            ctx.Emps.Add(emp);
            ctx.SaveChanges();
        }
    }
}

The output connection string would look like

Data Source=(localdb)\v11.0;Initial Catalog=ConsoleApplication1.HRContext;Integr ated Security=True;MultipleActiveResultSets=True

Namoskar!!!

Leave a Comment
  • Please add 3 and 8 and type the answer here:
  • Post
  • Excellent Article Wriju. I should tell dozens of Thanks to you. It has solved my problem.

Page 1 of 1 (1 items)