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 = "D开发者_JS百科ate 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.
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>());
Heres my current setup (replace caps with your details):
<connectionStrings>
<add name="TITLEContext" connectionString="metadata=res://*/Models.TITLE.csdl|res://*/Models.TITLE.ssdl|res://*/Models.TITLE.msl;provider=System.Data.SqlClient;provider connection string="Data Source=DATASOURCE;Initial Catalog=DATABASE;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
And then I followed up with this in the Public TITLEContext()
DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<TITLEContext>());
Took a couple tries, but put a break point on one of your loads and check the context's entities. It should have an optiont o see the database connection string...
good luck!
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
hope it helps
精彩评论