Getting code-first Entity Framework to build tables on SQL Azure
Posted
by
NER1808
on Stack Overflow
See other posts from Stack Overflow
or by NER1808
Published on 2012-03-29T11:27:37Z
Indexed on
2012/03/29
11:29 UTC
Read the original article
Hit count: 215
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
context.SaveChanges();
}
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!
© Stack Overflow or respective owner