问题
I have to use Firebird embeded database and Entity Framework. I have downloaded the Connector and if i use this code:
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();
}
Everything works. But when I try to use that connection string with 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; }
}
it fails with message:
Unsupported keyword: 'server type'
It looks like EF isn't using Firebird provider. How should I use it ?
回答1:
Your DbContext
should look like this:
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; }
}
You have to give it a clue it should be using FirebirdClient
.
回答2:
From the Firebird_v2.1.4.InstallationGuide.pdf from embeded package:
Client access can be only via the local (XNET) protocol, i.e. NOT a TCP/IP local loopback connection string that includes the server name “localhost” or the IP address 127.0.0.1. The embedded server supports only the local connect to an absolute database file path without a server name.
so this DataSource=localhost
is not supported (when I run that code i get the same error with Unsupported keyword: 'datasource'
)
But there is one more thing. The FAQ say (http://www.firebirdsql.org/en/firebird-net-provider-faq/#1):
- What versions of the MS.NET Framework are supported?
.NET 1.0, .NET 1.1, .NET 2.0 and the .NET Compact Framework 2.0
but in the download section says (http://www.firebirdsql.org/en/net-provider/):
October 5, 2013 - NETProvider-3.2.0.0.msi - 815 KB - FirebirdClient, Windows installer
October 5, 2013 - NETProvider-3.2.0.0-NET40.7z - 322 KB - FirebirdClient - .NET 4.0
October 5, 2013 - NETProvider-3.2.0.0-NET45.7z - 349 KB - FirebirdClient - .NET 4.5
So connector is for NET 4.0/4.5 but embeded says it supports only NET 2.0 ? I'm bit confused...
回答3:
I've been searching for something and found these resources. Hope it helps you:
Using the Firebird embedded server
User=SYSDBA;Password=masterkey;Database=SampleDatabase.fdb;DataSource=localhost; Port=3050;Dialect=3;Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=1;It's the key/value ServerType=1; that tells the driver it's in *embedded mode.*
And from the Firebird website: http://www.firebirdsql.org/en/net-examples-of-use/
来源:https://stackoverflow.com/questions/19235223/firebird-embeded-and-entityframework-code-first-whats-the-correct-way-to-set-c