我想在ASP.NET中創建一個簡單的項目,'代碼優先'。代碼優先 - 不創建數據庫
所以我做了一個類EFDbContext:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace MVCRubenWouters.Models
{
public class EFDbContext: DbContext
{
public EFDbContext() : base("name=rubenwoutersdbCF")
{
Database.SetInitializer<EFDbContext>(new EFDbInitializer());
}
public DbSet<Types> Types { get; set; }
}
}
而且在 'web.config中'
<connectionStrings>
<add name="rubenwoutersdbCF" connectionString="Data Source=.\SQLSERVER2012;Initial Catalog=rubenwoutersdbCF;Integrated Security=True;MultipleActiveResultSets=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
然後,我創建了一個類「EFDbInitializer的東西添加到數據庫中增加了一個ConnectionString的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MVCRubenWouters.Models
{
public class EFDbInitializer: DropCreateDatabaseAlways<EFDbContext>
{
protected override void Seed(EFDbContext context)
{
Types t1 = new Types()
{
PK_TypeNr = 1,
TypeKeuken = "Belgisch",
TypeZaak = "Restaurant",
Vegetarisch = false
};
context.Types.Add(t1);
context.SaveChanges();
}
}
}
當我運行該項目,並轉到SQL服務器管理工作室(和刷新),沒有ne w數據庫有..
我在這裏做錯了什麼? :/
您是否已將Mgmt Studio連接到'。\ SQLSERVER2012'實例? – 2014-12-27 15:19:55
@marc_s是的,我做到了。 – RW24 2014-12-27 15:30:35
嘗試調用實體框架,如果出現問題,您應該得到關於它的Exception。嘗試調用這個:'new EFDbContext()。Types.ToList();' – Dan 2014-12-27 16:20:36