I am trying out the MVC code first model.But,I dont see the database getting created when I run the code the first time.I am following DEV-B335 video on Entity Framework 6 by Rowan Miller.I have the same code as him.

My DBContext

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

My Models

 public class Blog
{
    [Key]
    public int Key { get; set; }
    public string Name { get; set;}
    public string Url { get; set; }

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

public class Post
{
    [Key]
    public int Key { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogKey { get; set; }
    public virtual Blog Blog { get; set; }
    public List<Comment> Comments { get; set; }
}

public class Comment
{
    [Key]
    public int Key { get; set; }
    public string Author { get; set; }
    public string AuthorUrl { get; set; }
    public string Content { get; set; }

    public int PostKey { get; set; }
    public Post Post { get; set; }

    public int StatusKey { get; set; }
    public Status Status { get; set; }
}

public class Status
{
    [Key]
    public int Key { get; set; }
    public string Name { get; set; }
    public bool DisplaytoPublic { get; set; }
}

Code for printing the output.

  static void CreateandPrintBlogs()
    {
        using (var db = new BloggingContext())
        {
            db.Blogs.Add(new Blog { Name = "Yet another Blog #" + (db.Blogs.Count() + 1) });
            db.SaveChanges();

            var blogs = (from b in db.Blogs
                         orderby b.Name
                         select b).ToList();

            Console.WriteLine("Blogs :");

            foreach (var item in blogs)
            {
                Console.WriteLine(" - "+item.Name);
            }
        }
    }

I have run the code once.I am getting the output,but I dont see the database in the SQL Server ObjectExplorer.Appreciate any help or any nudge in the right direction.

Related posts

Recent Viewed