0
votes

I tried to work with Code First approach in the new Entity Framework, but the table in the database is not automatically created.

I have already created database (in App_Code folder) and I use the Code First approach. Explain me, I need to create tables in DB first or they should be created automatically?

Below is a description of my project

In the Web.config I have 2 connection strings:

add name="ApplicationServices"
    connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    providerName="System.Data.SqlClient"

add name="MyDB"
   connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|MyDB.mdf;User Instance=true"
   providerName="System.Data.SqlClient"

Class describing my object (in Model folder)

public class UserProfile
{
        [Key]
        [Required]
        public string PhoneNumber { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string MiddleName { get; set; }

        [Required]
        public string Surname { get; set; }

        [Required]
        public DateTime Birthday { get; set; }

        public int PositionId { get; set; }

        public string Boss { get; set; }

        [Required]
        public int CompanyId { get; set; }
}

DbContext class (in Model folder)

public class MyDB : DbContext
{
    //public MyDB() : base("MyDB") { }
    public DbSet<UserProfile> UserProfiles { get; set; }
}

In the Controller trying to insert to the database new (& first) record (table not already exist).

UserProfile newProfile = new UserProfile();
newProfile.CompanyId = 1;
newProfile.PhoneNumber = model.PhoneNumber;
newProfile.FirstName = model.FirstName;
newProfile.MiddleName = model.MiddleName;
newProfile.Surname = model.Surname;
newProfile.Birthday = Convert.ToDateTime(model.Birthday);

var myDb = new Models.MyDB();
myDb.UserProfiles.Add(newProfile);
myDb.SaveChanges();

When I'm trying to call SaveChanges() I get an exception:

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.UserProfiles'.

1
try removing both connection strings. Actually it should automatically create database. - Praveen Prasad
But if I already have databases with some tables & data in it & wand to add another tables with code first approach? This approach didn't work? - Pavel
If I create table UserProfiles manual - all Ok and new record inserted. But I want Code First :) - Pavel

1 Answers

1
votes

Built-in EF database initializers must create a database and all tables in the database - it is not able to work with existing database but you can build your own initializer which will be able to create tables in existing database.

Code first with database creation currently expects that you can delete and recreated database as many times as you need because no database initializer supports altering existing tables (this will be solved by very promising Migrations).