2012-03-29 32 views
2

我是新的代碼優先的實體框架。獲取代碼優先的實體框架以在SQL Azure上構建表格

我已經嘗試了一些東西,但無法獲得EF構建我的SQL Azure數據庫中的任何表。 任何人都可以建議我應該檢查一些步驟和設置。

成員資格提供者在創建表格時沒有問題。

我在連接字符串中添加了PersistSecurityInfo = True。連接字符串正在使用服務器的主用戶帳戶。

當我使用sql實現數據庫中的表時,一切正常。

我在WebRole.cs以下

  //Initialize the database 
     Database.SetInitializer<ReykerSCPContext>(new DbInitializer()); 

我DbInitializer(這之前,我得到一個「無效的對象名稱dbo.ClientAccountIFAs'。」當我嘗試訪問該表沒有得到運行首次。有時後啓動。

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(); 
     } 

和我的DbContext類是什麼樣子

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. 

    } 

用於訪問的代碼是

 public List<ClientAccountIFA> GetAllClientAccountIFAs() 
    { 
     using (DataContext) 
     { 

      var caiCollection = from c in DataContext.ClientAccountIFAs 
           select c; 

      return caiCollection.ToList(); 
     } 
    } 

它在最後一行發生錯誤。 幫助!

回答

0

這裏發現的問題與我正在使用的通用會員供應商混淆在一起。 成員資格提供者在實體框架之前實例化數據庫,以便每次EF認爲數據庫存在並且不會試圖放棄它。

爲了解決這個問題,我確信我不僅在WebRole.cs的OnStart方法中設置了初始值設定項,而且還強制查詢到了DbContext。

然後這就迫使數據庫在會員表之前創建,一切都很好。 第一次調用成員資格提供程序時,會創建必需的表。