I am new the code-first Entity Framework.
I have tried a few things now, but can’t get EF to construct any tables in the my SQL Azure database.
Can anyone advise of some steps and settings I should check.
The membership provider has no problems create it’s tables.
I have added the PersistSecurityInfo=True in the connection string. The connection string is using the main user account for the server.
When I implement the tables in the database using sql everything works fine.
I have the following in the WebRole.cs
//Initialize the database
Database.SetInitializer<ReykerSCPContext>(new DbInitializer());
My DbInitializer (which does not get run before I get a “Invalid object name ‘dbo.ClientAccountIFAs’.” when I try to access the table for the first time. Sometime after startup.
public class DbInitializer:DropCreateDatabaseIfModelChanges<ReykerSCPContext>
{
protected override void Seed(ReykerSCPContext context)
{
using (context)
{
//Add Doc Types
context.DocTypes.Add(new DocType() { DocTypeId = 1, Description = "Statement" });
context.DocTypes.Add(new DocType() { DocTypeId = 2, Description = "Contract note" });
context.DocTypes.Add(new DocType() { DocTypeId = 3, Description = "Notification" });
context.DocTypes.Add(new DocType() { DocTypeId = 4, Description = "Invoice" });
context.DocTypes.Add(new DocType() { DocTypeId = 5, Description = "Document" });
context.DocTypes.Add(new DocType() { DocTypeId = 6, Description = "Newsletter" });
context.DocTypes.Add(new DocType() { DocTypeId = 7, Description = "Terms and Conditions" });
//Add ReykerAccounttypes
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 1, Description = "ISA" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 2, Description = "Trading" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 3, Description = "SIPP" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 4, Description = "CTF" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 5, Description = "JISA" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 6, Description = "Direct" });
context.ReykerAccountTypes.Add(new ReykerAccountType() { ReykerAccountTypeID = 7, Description = "ISA & Direct" });
//Save the changes
base.Seed();
}
and my DBContext class looks like
public class ReykerSCPContext : DbContext
{
//set the connection explicitly
public ReykerSCPContext():base("ReykerSCPContext"){}
//define tables
public DbSet<ClientAccountIFA> ClientAccountIFAs { get; set; }
public DbSet<Document> Documents { get; set; }
public DbSet<DocType> DocTypes { get; set; }
public DbSet<ReykerAccountType> ReykerAccountTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Runs when creating the model. Can use to define special relationships, such as many-to-many.
}
The code used to access the is
public List<ClientAccountIFA> GetAllClientAccountIFAs()
{
using (DataContext)
{
var caiCollection = from c in DataContext.ClientAccountIFAs
select c;
return caiCollection.ToList();
}
}
and it errors on the last line.
Help!
The problem here transpired to be mixed up with the Universal Membership Provider I was using.
The membership provider was instantiating the database before Entity Framework, so that each time EF thought the database existed and did not try to drop it.
To solve this I made sure that not only did I set the initializer in the OnStart method of the WebRole.cs, but to also force a query to the DbContext too.
This then forced the database to be created prior to the membership tables and all was well.
Once the membership provider is called for the first time, it creates the tables that are necessary.