Firebird嵌入和EntityFramework代码第一:设置连接字符串和提供程序的正确方法是什么? [英] Firebird embeded and EntityFramework code first: what's the correct way to set connection string and provider?

查看:186
本文介绍了Firebird嵌入和EntityFramework代码第一:设置连接字符串和提供程序的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Firebird嵌入式数据库和Entity Framework。我已经下载了连接器,如果我使用这个代码:

 使用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 boolean
}

public DbSet< ItemA> ItemA {get;组; }
public DbSet< ItemB> ItemsB {get;组;
}

它失败了消息:

 不支持的关键字:'服务器类型'

看起来EF没有使用Firebird提供程序。我应该如何使用它?

解决方案

您的 DbContext 应该像这样:

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

public DbSet< ItemA> ItemA {get;组; }
public DbSet< ItemB> ItemsB {get;组; }
}

你必须给出一个线索,它应该使用 FirebirdClient


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 ?

解决方案

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.

这篇关于Firebird嵌入和EntityFramework代码第一:设置连接字符串和提供程序的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆