一個code first 入門的代碼哦。根據(jù)園子里的代碼敲的,我代碼運行成功了。但在數(shù)據(jù)庫沒有生成數(shù)據(jù)庫。
園子里這樣說:
當(dāng)沒有顯示設(shè)置數(shù)據(jù)連接的時候,默認的數(shù)據(jù)庫是:.\SQLEXPRESS。如果本地沒有SQLEXPRESS,EF會嘗試LocalDb ((localdb)\v11.0) .\SQLEXPRESS
?
這句話的意思是本地沒有 .\SQLEXPRESS就會嘗試LocalDb ((localdb)\v11.0) .\SQLEXPRESS是嗎,但我本地是沒有.\SQLEXPRESS這個呢。我鏈接數(shù)據(jù)庫的服務(wù)器名稱就是一個點 .? 那上面那句話EF就會這樣找嗎?((localdb)\v11.0) .\SQLEXPRESS
那不是還在找這個嗎?.\SQLEXPRESS 我沒有。那不還是找不到。會的幫我解決下。謝謝了。
?
補充一下:我是vs2012開發(fā)工具,如果我導(dǎo)入EF4.0就會報錯:提供程序未返回 ProviderManifestToken 字符串。但我通過NuGet安裝的,這個安裝的是EF5
這樣就不報錯。執(zhí)行成功。數(shù)據(jù)庫沒生成數(shù)據(jù)庫。會的也幫我解答下。謝謝。我是剛學(xué)者弄EF。
?
Model類
View Code
///
/// Model
///
public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
public List Lodgings { get; set; }
}
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public Destination Destination { get; set; }
}
?
Context
View Code
///
/// Context
///
public class BreakAwayContext : DbContext
{
public DbSet Destinations { get; set; }
public DbSet Lodgings { get; set; }
}
?
Main
View Code
static void Main(string[] args)
{
var destination = new Destination
{
Country = "Indonesia",
Description = "EcoTourism at its best in exquisite Bali",
Name = "Bali"
};
using (var context = new BreakAwayContext())
{
context.Destinations.Add(destination);
context.SaveChanges();
}
Console.WriteLine("OK");
Console.ReadKey();
}
1 回答

皈依舞
TA貢獻1851條經(jīng)驗 獲得超3個贊
你可以這么認為,當(dāng)你計算機中有本地數(shù)據(jù)庫比如你安裝了sql2008那它就會自動認為這個是你的數(shù)據(jù)庫,沒有話連接sql exress,再沒有連接localdb,localdb是屬于sql2012的一個比sql express更輕量級的數(shù)據(jù)庫。
ef不用非得用nuget安裝,其實你引用那個dll效果是一樣。
- 1 回答
- 0 關(guān)注
- 500 瀏覽
添加回答
舉報
0/150
提交
取消