jeudi 29 mai 2014

Create tables from code first with Entity Framework 6


Vote count:

0




I'd like completely delete table (and data) and recreate the table.


I tried to create the table with this : [TestMethod]



public void TestMethod1()
{
using (var db = new MyContext())
{
db.Customers.ToList();
}
}


But I get this error : System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'dbo.Customers'.



public class Customer
{
public int CustomerID { get; set; }
public string LastName { get; set; }
}

public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
// Primary Key
this.HasKey(t => t.CustomerID);

// Properties
this.Property(t => t.CustomerID)
.IsRequired();

this.Property(t => t.LastName)
.IsRequired()
.HasMaxLength(40);
}
}

public partial class MyContext : DataContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}

public MyContext() : base("Name=MyContext") { }

public DbSet<Customer> Customers { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CustomerMap());
}
}


The connection string is :



<connectionStrings>
<add
name="MyContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True" />
</connectionStrings>


asked 1 min ago

Kris-I

3,689





Aucun commentaire:

Enregistrer un commentaire