1 Replies - 5501 Views - Last Post: 11 February 2013 - 09:41 AM

#1 DimiBy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-September 11

No new tables are added to the database with code first EF

Posted 31 January 2013 - 02:38 PM

Hey, guys.

I have next classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using SearchPets.Models.Entities;


namespace SearchPets.Models.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Advert> Adverts { get; set; }
        public DbSet<Kind> Kinds { get; set; }
        public DbSet<Age> Ages { get; set; }
        public DbSet<Castration> Castrations { get; set; }
        public DbSet<Coloring> Colorings { get; set; }
        public DbSet<Color> Colors { get; set; }
        public DbSet<Hair> Hair { get; set; }
        public DbSet<Pet> Pets { get; set; }
        public DbSet<Sex> Sexes { get; set; }
        
    }
}


<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SearchPets;Integrated Security=True"
          providerName="System.Data.SqlClient"/>
  </connectionStrings>


After run the application, opens a database SearchPets , but for some reason the tables are not created. Where is the mistake?

Thanks for advance

Is This A Good Question/Topic? 0
  • +

Replies To: No new tables are added to the database with code first EF

#2 BattlFrog  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-April 12

Re: No new tables are added to the database with code first EF

Posted 11 February 2013 - 09:41 AM

Hi DimiBy. If that is all the code you have, you have not actually created anything yet. It looks like you are doing code first, so you need to spell out the model properties, then you have to put in some controller code, to make it all do stuff. Here are some excerpts copied from: http://msdn.microsof...a/jj193542.aspx


DbContext:

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






Define the properties:

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; }
}




Do stuff with it:

class Program
{
    static void Main(string[] args)
    {
        using (var db = new BloggingContext())
        {
            // Create and save a new Blog
            Console.Write("Enter a name for a new Blog: ");
            var name = Console.ReadLine();
 
            var blog = new Blog { Name = name };
            db.Blogs.Add(blog);
            db.SaveChanges();
 
            // Display all Blogs from the database
            var query = from b in db.Blogs
                        orderby b.Name
                        select b;
 
            Console.WriteLine("All blogs in the database:");
            foreach (var item in query)
            {
                Console.WriteLine(item.Name);
            }
 
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}




I recommend going to the link I provided above, as it has a video, etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1