none
I can't find where EF created the database RRS feed

  • Question

  • HI -- am a new to MVC 3 which uses EF.   I have created code and I have verified that the data  is being persisited.   I just can't find out where the database was created.  I have look with MS SQL Manage studio for both sqlexpress and sqlexpress compact edition.  

    I just used the default setting and let EF create the db.. any help pointing me in the right direction will be very useful.

     


    Howard Davis
    Monday, November 21, 2011 3:20 AM

Answers

All replies

  • > am a new to MVC 3 which uses EF. I have created code and I have verified that the data is being persisited. I just can't find out where the database was created.


    the path to the database could be written in the web.config, connectionsStrings tag
    Monday, November 21, 2011 4:42 AM
  • Hi,

    If you are using Code First, the path to the database is a bit different. It's the fully qualified name of your context. So if your context is named MyDBContext in the namespace MyNamespace the name could be Mynamespace.MyDBContext.

    Take a look at http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx section 5 (Where's my data) and section 7 about your database.


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.
    Monday, November 21, 2011 6:49 AM
  • If I set a connection string correctly and use the name with the exact name of my class derived from data context I can add the data base to a SQLServer.   thats is OK

     

    But I still cannot find the defaykt SQL database in SQLExpress. Nor can I find *.sdf with the correct access time.

     

    I am using the  server Managerment studio....

     

     

     


    Howard Davis
    Tuesday, November 22, 2011 2:17 AM
  • > But I still cannot find the defaykt SQL database in SQLExpress.


     
    let's suppose you have ASP.NET MVC solution with a following
     
      

    [Web.config]
    <?xml version="1.0"?>
    <configuration>
      <connectionStrings>
        <add name="ApplicationServices"
             connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
             providerName="System.Data.SqlClient" />
      </connectionStrings>
      ...
    


     
    [Models\Entities.cs]

    using System.Data.Entity;
    
    namespace MvcApplication2.Models
    {
        public class Entity
        {
            public int Id { get; set; }
        }
    
        public class Data : System.Data.Entity.DbContext
        {
            public DbSet<Entity> Entities { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder mb)
            {
                base.OnModelCreating(mb);
                mb.Entity<Entity>().HasKey(t => t.Id).ToTable("Entities");
            }
        }
    }
    

     
    [Controllers\HomeController.cs]
    using System.Data;
    using System.Data.Entity;
    using System.Web.Mvc;
    
    namespace MvcApplication2.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                var d = new MvcApplication2.Models.Data();
                ...
    
     
    in order to get the physical path to the database file, you can write the following
     
    var path = d.GetPath();
    
    

     
    below is an implementation of the GetPath extension method
     
    public static class DbContextHelper
    {
        public static string GetPath(this DbContext dc)
        {
            dc.Database.CreateIfNotExists();
            dc.Database.Connection.Open();
            try
            {
                var c = dc.Database.Connection.CreateCommand();
                c.CommandText = "SELECT filename FROM master..sysdatabases WHERE name='" + dc.GetType().FullName + "'";
                c.CommandType = CommandType.Text;
                return (string) c.ExecuteScalar();
            }
            finally
            {
                dc.Database.Connection.Close();
            }
        }
    }
    
          
    • Edited by Malobukv Tuesday, November 22, 2011 5:20 AM
    Tuesday, November 22, 2011 5:09 AM
  • Hi,

    Well it should be there if you don't do anything special.

    Could you post your connectionstring in the web.config and the DbContext definition (including it's namespace)?


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.
    Tuesday, November 22, 2011 6:35 AM
  • Hi Howard,

    I think you can refer this link here: http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 24, 2011 8:16 AM
    Moderator