Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First CTP 5 and SQL SErver 2008 R2

I can't seem to get the EF Code First to work with SQL Server 2008 R2. The error I am getting is "Invalid object name 'dbo.Movies'."

It is not creating the table automatically.

My connection string:

<add name="MovieDBContext" connectionString="Server=(local); Database=Movies; Trusted_Connection=true; Integrated Security=True" providerName="System.Data.SqlClient" />

My model and context class:

public class Movie
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Title is required")]
    public string Title { get; set; }

    [Required(ErrorMessage = "Date is required")]
    public DateTime ReleaseDate { get; set; }

    [Required(ErrorMessage = "Genre must be specified")]
    public string Genre { get; set; }

    [Required(ErrorMessage = "Price Required")]
    [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
    public decimal Price { get; set; }

    [StringLength(5)]
    public string Rating { get; set; }

}

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Movie>().Property(p => p.Price).HasPrecision(18, 2);
    }
}

Any help would be much appreciated.

like image 293
Cliffboss Avatar asked Feb 18 '11 20:02

Cliffboss


People also ask

How do I convert code first to database first?

There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away.


1 Answers

I forget if it's enabled by default but try setting this in your Application_Start (pretty sure it's not)

System.Data.Entity.Database.DbDatabase.SetInitializer<MovieDBContext>(new CreateDatabaseIfNotExists<MovieDBContext>());
like image 184
Buildstarted Avatar answered Nov 06 '22 18:11

Buildstarted