2

I'll try to use CodeFirst model for creating dataBase.

Prepare code like below (from online tutorial). Code is exactly like in tutorial:

class Program
{
    static void Main(string[] args)
    {
        using (BlogContext db = new BlogContext())
        {
            Console.Write("Enter a name for a new Blog:");
            var name = Console.ReadLine();

            var blog = new Blog { Name = name };
            Database.SetInitializer<BlogContext>(null);
            db.Blogs.Add(blog);
            db.SaveChanges();

            var query = from b in db.Blogs
                        orderby b.Name
                        select b;

            foreach (var item in query)
            {
                Console.WriteLine(item.Name);
            }

        }
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Name { get; set; }

    public virtual List<Post> Posts { get; set; }

}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public virtual Blog Blog { get; set; }
}

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}

As result - got exception

enter image description here

Solution for this exception found here. Now it's ok, work and show me saved data, but in my Sql Server Object explorer i can't see any database or table.

enter image description here

Any suggestion why? Wnat i must to do for repairing this?


Edit

enter image description here

So, as was mentioned by @Sergiy Berezovskiy - i try to check dataBase and add manually dataConnection for displaying just created dataBase.

enter image description here

Community
  • 1
  • 1
hbk
  • 10,908
  • 11
  • 91
  • 124

1 Answers1

2

I think you have two problems:

  • You are looking on wrong database. Without connection string specified Entity Framework will create database on local SQLEXPRESS server with name equal to context name - SomeNamespace.BlogContext in your case.
  • You changed model after you created database. That's why you see that error. Either use Code First Migrations to update existing database, or use DropCreateDatabaseWhenModelChanges database initializer to re-create database.
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Regarding first point - please see Edit (add additional screen), also according to tutorial i don't create any connection string, i check .config file - no connection string there. Do I must to add it manually for proper work? Regarding second point - i just read about migration and add all required changes. But the main my question now - why i can't see my created database? – hbk Jan 19 '14 at 19:01
  • oh, i try manually to make new dataConnection and found created database in list, so now all it's ok. But I have one more question Do i always must to manually add dataConnection for just created data base or it's can be added automatically? – hbk Jan 19 '14 at 19:10
  • @Kirill unfortunately you should add connection manually. Even with Sql Server Management Studio you should refresh database list to see new database arrived – Sergey Berezovskiy Jan 19 '14 at 19:12