簡體   English   中英

使用實體框架創建數據庫

[英]Create database with Entity Framework

我正在嘗試使用實體框架創建一個名為Todo的數據庫。 我希望此數據庫在創建后出現在Sql Server管理中。

我有上下文文件:

namespace Domain
{
    //Associate the model with the database
    //This class then automatically defines a property for each table in the database that I want to work with.
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("EFDbContext")
        {
        }
        public DbSet<List> Lists { get; set; }
        public DbSet<Task> Tasks { get; set; }
    }
}

我有我的消毒器:

namespace Domain
{
    public class ListRepository : System.Data.Entity.DropCreateDatabaseIfModelChanges<EFDbContext>
    {
        protected override void Seed(EFDbContext context)
        {
            var todo = new List<List>
            {
                new List { Day="Månadg" },
                new List { Day="Tisdag" },
                new List { Day="Onsdag" },
                new List { Day="Torsdag" },
                new List { Day="Fredag" }
            };

            todo.ForEach(t => context.Lists.Add(t));
            context.SaveChanges();
        }
    }
}

這是我的Web.Config:

  <connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Todo;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

    <contexts>
      <context type="todo.domain.EFDbContext, todo">
        <databaseInitializer type="todo.domain.ListRepository, todo"></databaseInitializer>
      </context>
    </contexts>

運行應用程序時,將在服務器資源管理器中創建數據連接,如下圖所示:

阿斯

當我單擊此數據連接時,出現以下錯誤消息: 輸入此處的圖像

當我在Sql Server Managment中簽入時,那里沒有列出數據庫。 我在這里做錯了什么? 為什么不創建dabatabase?

如您所見,我有一個名為Todo的解決方案,在Todo-solution中,我有三個項目。 我在域項目中有DbContext

從您的屏幕截圖中可以看到,消息中用戶Bryan的登錄失敗。 這意味着Bryan無法連接

檢查以下3項

  1. 在Sql Management Studio中,打開Db,右鍵單擊圖標,然后向下滾動至安全性(通常在Windows身份驗證模式下最簡單),然后向下滾動至安全性,然后add Bryan as a user giving him DBO rights
  2. Authentication type檢查是否啟用了混合模式身份驗證
  3. 將連接切換到身份驗證並嘗試使用SA角色

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM