locked
No LocaDb or SQL Server Express database generated while using EF Code first RRS feed

  • Question

  • I just follow the instruction to run the program. see the instruction below:

    https://msdn.microsoft.com/en-us/data/jj591621.aspx

    Contents in Mode.cs:

    using System.Data.Entity; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.Data.Entity.Infrastructure; 
    
    namespace MigrationsDemo 
    { 
        public class BlogContext : DbContext 
        { 
            public DbSet<Blog> Blogs { get; set; } 
        } 
    
        public class Blog 
        { 
            public int BlogId { get; set; } 
            public string Name { get; set; } 
        } 
    }

    Contents in Program.cs :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MigrationsDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var db = new BlogContext())
                {
                    db.Blogs.Add(new Blog { Name = "Another Blog " });
                    db.SaveChanges();
    
                    foreach (var blog in db.Blogs)
                    {
                        Console.WriteLine(blog.Name);
                    }
                }
    
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(); 
            }
        }
    }

    The instruction said now run the program and I will see that a MigrationsCodeDemo.BlogContextdatabase is created for you.

    In the mean time, the instruction said:

    If SQL Server Express is installed (included in Visual Studio 2010) then the database is created on your local SQL SErver Express instance (.\SQLEXPRESS).

    If SQL Server Express is not installed then code-first will try and use LocalDb ((localdb)\v11.0) - LocalDb is included with Visual Studio 2012.

    But after running this program, I cannot see MigrationsCodeDemo.BlogContext having been created in SQL Server Object Explorer, I just see system databases under databases folder, and the command line shows:

    Another Blog
    Another Blog
    Another Blog
    Another Blog
    Press any key to exit...

    since I run 4 times for this program, so there are 4 lines "Another Blog" appeared in the command line.

    Can anyone tell me why the code didn't generate the Local DB for me?

    following is the structure of the LocaDb Database:

    Thanks a lot.

    Monday, February 9, 2015 6:21 AM

Answers

  • That you are using the local SQLEXPRESS instance, not localdb


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Proposed as answer by ErikEJMVP Thursday, February 12, 2015 12:57 PM
    • Marked as answer by zhexin Thursday, February 12, 2015 2:44 PM
    Thursday, February 12, 2015 12:57 PM

All replies

  • Find the actual connection string used by looking at:

         db.Database.Connection.ConnectionString


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com


    • Edited by ErikEJMVP Monday, February 9, 2015 10:10 AM
    Monday, February 9, 2015 10:10 AM
  • Find the actual connection string used by looking at:

         db.Database.Connection.ConnectionString


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com


    Sorry, I'm a beginner, so I'm not very clear what's your meaning for looking at : db.Database.Connection.ConnectionString

    can you give me a more detailed steps about how to check this string?

    Monday, February 9, 2015 2:10 PM
  • Add this line after db.SaveChanges:

    System.Console.WriteLine(db.Database.Connection.ConnectionString);


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Monday, February 9, 2015 4:20 PM
  • Add this line after db.SaveChanges:

    System.Console.WriteLine(db.Database.Connection.ConnectionString);


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Hi,

    I got following output:

    Data Source=.\SQLEXPRESS;Initial Catalog=MigrationsDemo.BlogContext;Integrated Security=True;MultipleActiveResultSets=True

    what does this result indicate?

    Thursday, February 12, 2015 3:28 AM
  • That you are using the local SQLEXPRESS instance, not localdb


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Proposed as answer by ErikEJMVP Thursday, February 12, 2015 12:57 PM
    • Marked as answer by zhexin Thursday, February 12, 2015 2:44 PM
    Thursday, February 12, 2015 12:57 PM