ksg
ksg

Reputation: 4067

Database not getting created on EntityFramework-CodeFirst Approach

I am new to Entityframework.I am trying to create database using Codefirst approach of entityFramework. I have my Code in the DAL Layer here :

 [Table("Category")]
public class Category
{
    [Key]
    public int CategoryId { get; set; }

    [Required]
    [Column(TypeName="varchar")]
    [StringLength(200)]
    public string CategoryName { get; set; }
}


public class DatabaseContext:DbContext
{
    public DatabaseContext()
        : base("DbConnection")
    {
      //Added the following but still it doesnt work
      //Database.SetInitializer<DatabaseContext>(new CreateDatabaseIfNotExists<DatabaseContext>());
    }

    public DbSet<Category> Categories { get; set; }
}

Connection string in the App.Config file of DAL Layer

<connectionStrings>
<add name="DbConnection"
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=dbPdtCgry;uid=admin;password=admin123;"
      providerName="System.Data.SqlClient"></add>

This does not create database in Sql server management.But DbConnection Log file & mdf file is created in the the App_Data and I am able to add,update and delete data.

Am I missing any thing here??

Upvotes: 1

Views: 87

Answers (2)

MOSCODE
MOSCODE

Reputation: 292

I always seed like this step by step then i know that i am not missing anything.

   <connectionStrings>
    <add name="LibraryDb" connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LibraryDb.mdf;Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

My Classes

 public class Author
{
    [Key]
    public int id { get; set; }        
    [Required(ErrorMessage = "You Need to Enter A Author Name ")]
    public string AuthorName { get; set; }
    public string Address { get; set; }
    public virtual List<book> books{ get; set; }

}
public class Book
{
    public int id { get; set; }
    public string BookName { get; set; }
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DateofPublish { get; set; }
    public int id { get; set; }

    public virtual Author Author{ get; set; }
}

public class LibraryDb: DbContext
{
    public DbSet<Author> Authors{ get; set; }
    public DbSet<Book> Books{ get; set; }

    public LibraryDb() : base("LibraryDb") { }

}

Then in my Intialiser Class i do this to seed

public class LibraryInitiliser:DropCreateDatabaseAlways<LibraryDb>
{
    protected override void Seed(LibraryDb context)
    {
        var book= new List<Author>
        {
            new Author() { AuthorName = "Naame1", Address = "dublin", Book= new List<Book>
            {
                new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981")  },
                 new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981")  },
                new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981")  }
            } },
            new Author() { AuthorName = "Naame1", Address = "dublin", Book= new List<Book>
            {
                new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981")  },
                 new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981")  },
                new Book() { BookName = "Naae1", PublishDate= DateTime.Parse("01/02/1981")  }
            } },

            } }

        };

        book.ForEach(m => context.Library.Add(m));
        context.SaveChanges();

        base.Seed(context);
    }
}

And Remeber in the global.asax to add

Database.SetInitializer(new LandLordInitiliser());

otherwise no data will be seeded.

Hope That Helps

Upvotes: 1

Nick
Nick

Reputation: 443

may be you missed Server=localhost\SQLserverInstanceName in connection string, because of it DB was created on your App_data but not in default folder of your SQL server

Upvotes: 1

Related Questions