2017-10-10 51 views
0

新手在這裏。如何在WebApi中使用實體框架和代碼優先創建數據庫

問題:

當我打開從VS2017菜單中的SQL Server對象資源管理器,我沒有找到我的數據庫稱爲UserRegistryDB

請幫忙。我做了以下。

  1. 我已經安裝了SQL Server 2017 EXPRESS

  2. 我添加實體框架項目引用。

  3. 我在Model文件夾

    public class UserDBContext : DbContext 
    {      
        public DbSet<User> Users { get; set; } 
    } 
    
  4. 我在Model文件夾中添加了一個模型類創建一個類UserDBContext.cs

    public class User 
    { 
            public int Id { get; set; } 
            public string emailID { get; set; } 
            public string AppName { get; set; } 
            public string SessionToken { get; set; } 
    } 
    
  5. 我在web.config添加了這個:

    <connectionStrings> 
        <add name="UserDBContext" 
          connectionString="server=.; database= UserRegistryDB; integrated security = true;" 
             providerName ="System.Data.SqlClient"/> 
      </connectionStrings> 
    
  6. 我建立我的項目並運行它;沒有編譯錯誤,建立成功。

我需要幫助的問題:

什麼是使用服務器=在連接字符串的差異。 ,服務器=(本地)?或服務器=(的LocalDB)

connectionString="server=. ; database= UserRegistryDB; integrated security = true;" 

connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TestDatabase;Integrated Security=True" 

感謝

+1

可能值得閱讀文檔https://docs.microsoft.com/en-us/ ef/core/get-started/ – series0ne

回答

0

.(local)指安裝默認實例 - 實例沒有任何具體的實例名(將在SQL Server配置管理器顯示爲MSSQLSERVER來)。這可能是SQL Server的任何版本 - 快遞,網絡,標準,企業,開發者等

(localdb)是指安裝了SQL Server 快速的LocalDB實例 - 這是SQL Server Express中的面向開發人員的版本,ISN」 t作爲Windows服務安裝並始終運行,但是隻能在Visual Studio或應用程序運行時啓動並運行

+0

我想知道我做錯了什麼嗎?我需要這樣做,我可以創建數據庫和表。我需要你的幫助。 – MilkBottle

相關問題