2013-10-07 25 views
1

我必須使用Firebird嵌入式數據庫和實體框架。我已經下載連接器,如果我使用此代碼:Firebird嵌入和EntityFramework代碼優先:設置連接字符串和提供者的正確方法是什麼?

using FirebirdSql.Data.FirebirdClient; 

[...] 

string exePath = Path.GetDirectoryName(
    new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath); 

FbConnectionStringBuilder fbStringBuilder = new FbConnectionStringBuilder(); 
fbStringBuilder.ServerType = FbServerType.Embedded; 
fbStringBuilder.UserID = "SYSDBA"; 
fbStringBuilder.Password = "MASTERKEY"; 
fbStringBuilder.Dialect = 3; 
fbStringBuilder.Charset = "UTF8"; 
fbStringBuilder.ClientLibrary = Path.Combine(exePath, "fbembed.dll"); 
fbStringBuilder.Database = Path.Combine(exePath, "test.fdb"); 


if (!File.Exists(Path.Combine(exePath, "test.fdb"))) 
{ 
    FbConnection.CreateDatabase(fbStringBuilder.ToString()); 
} 

FbConnection fbConn = new FbConnection(fbStringBuilder.ToString()); 

try 
{ 
    fbConn.Open(); 

    Console.WriteLine("OK"); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine("ERROR"); 
    Console.WriteLine(ex.Message); 
    Console.ReadKey(); 
} 
finally 
{ 
    fbConn.Close(); 
} 

一切正常。但是,當我嘗試使用該連接字符串的DbContext:

public class FirebirdEmbededExampleDbContext : DbContext 
{ 
    public FirebirdEmbededExampleDbContext(string connString) : base(connString) 
    { 
     this.Database.Connection.ConnectionString = connString; 
    } 

    public DbSet<ItemA> ItemsA { get; set; } 
    public DbSet<ItemB> ItemsB { get; set; } 
} 

它失敗消息:

Unsupported keyword: 'server type' 

它看起來像EF沒有使用火鳥提供商。我應該如何使用它?

回答

2

DbContext應該是這樣的:

public class FirebirdEmbededExampleDbContext : DbContext 
{ 
    public FirebirdEmbededExampleDbContext(string connString) 
     : base(new FbConnection(connString), true) 
    { } 

    public DbSet<ItemA> ItemsA { get; set; } 
    public DbSet<ItemB> ItemsB { get; set; } 
} 

你必須給它,它應該使用FirebirdClient線索。

0

我一直在尋找的東西,發現這些資源。希望它可以幫助你:

使用火鳥嵌入式服務器
用戶= SYSDBA;密碼= masterkey;數據庫= SampleDatabase.fdb;數據源=本地主機; Port = 3050; Dialect = 3; Charset = NONE; Role =;連接 lifetime = 15; Pooling = true; MinPoolSize = 0; MaxPoolSize = 50;數據包 Size = 8192; ServerType = 1;

這是鍵/值ServerType = 1;告訴它的驅動程序 *嵌入模式*

而且從火鳥網站: http://www.firebirdsql.org/en/net-examples-of-use/

1

從從嵌入式封裝Firebird_v2.1.4.InstallationGuide.pdf:

客戶端訪問只能通過本地(XNET)協議進行,即不包含服務器名稱爲「localhost」或IP地址127.0.0.1的TCP/IP本地環回連接字符串。嵌入式服務器僅支持本地連接到不帶服務器名稱的絕對數據庫文件路徑。

所以這DataSource=localhost不支持(當我運行的代碼我得到Unsupported keyword: 'datasource'同樣的錯誤)

但有一兩件事。常見問題解答說(http://www.firebirdsql.org/en/firebird-net-provider-faq/#1):

  1. 什麼MS.NET Framework版本支持?

.NET 1.0,.NET 1.1,.NET 2.0和.NET Framework精簡2.0

但在下載區說(http://www.firebirdsql.org/en/net-provider/):2013

10月5日 - NETProvider-3.2.0.0.msi - 815 KB - FirebirdClient,Windows安裝程序

2013年10月5日 - NETProvider-3.2.0.0-NET40.7z - 322 KB - FirebirdClient - .NET 4。0

2013年10月5日 - NETProvider-3.2.0.0-NET45.7z - 349 KB - FirebirdClient - .NET 4.5

因此連接器是.NET 4.0/4.5,但內嵌說,它僅支持NET 2.0 ?我有點糊塗了......

相關問題