Entity Framework CodeFirst : Where is my database
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!!!
Comments
Anonymous
July 15, 2014
Excellent Article Wriju. I should tell dozens of Thanks to you. It has solved my problem.Anonymous
January 19, 2015
Thank you very much, I've been looking for this for days now. Why is this so hidden? Surely, I'm not the first person who wants to have a look at the data.