【问题标题】:"Unknown data type" error with Firebird embedded and Entity Framework 6Firebird 嵌入式和实体框架 6 出现“未知数据类型”错误
【发布时间】:2014-01-06 21:18:35
【问题描述】:

我正在使用带有代码的嵌入式 Firebird 数据库(实体框架 6)。应用程序第一次运行时,它运行良好:创建数据库并插入数据。但之后每次运行时,都会抛出以下异常:

“System.NotSupportedException”类型的异常发生在 FirebirdSql.Data.FirebirdClient.dll 但未在用户代码中处理

附加信息:未知数据类型

该项目包括以下 NuGet 包:

  • EntityFramework [6.0.2]
  • Firebird ADO.NET 数据提供程序(实体框架 6)[4.1.0.0]

here 所述,我将DbProviderFactoriesFirebirdSql.Data.FirebirdClient 提供程序添加到App.config

我还将 Firebird DLL 添加到项目中并将它们设置为复制到输出目录:

  • fbembed.dll
  • ib_util.dll
  • icudt30.dll
  • icuin30.dll
  • icuuc30.dll

还没有 enabled 代码优先迁移(尽管__MigrationHistory 表仍因某种原因被创建)。

代码如下:

class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer<FirebirdDbContext>(new CreateDatabaseIfNotExists<FirebirdDbContext>());
        string connectionString = "server type=Embedded;user id=sysdba;password=masterkey;role name=RDB$ADMIN;character set=UTF8;initial catalog=test.fdb";

        using (var context = new FirebirdDbContext(connectionString))
        {
            context.Users.Add(new User()
                { Created = DateTime.Now,
                    Name = "smith" });
            context.SaveChanges();
        }
    }
}

class User
{
    [Key]
    public DateTime Created { get; set; }

    public string Name { get; set; }
}

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

    public DbSet<User> Users { get; set; }
}

class MyConfiguration : DbConfiguration
{
    public MyConfiguration() 
    {
        SetDefaultHistoryContext((c, s) => new SmallKeyHistoryContext(c, s));
    }
}

class SmallKeyHistoryContext : HistoryContext
{
    public SmallKeyHistoryContext(DbConnection existingConnection, string defaultSchema)
        : base(existingConnection, defaultSchema) { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // HACK: Make this column smaller to avoid the following error:
        // key size exceeds implementation restriction for index "PK___MigrationHistory"
        modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(53).IsRequired();
    }
}

context.Users.Add(...) 行引发异常。

这是堆栈跟踪

at FirebirdSql.Data.Common.DbValue.GetBytes() in c:\Users\Jiri\Documents\devel\NETProvider\working\NETProvider\source\FirebirdSql\Data\Common\DbValue.cs:line 315
at FirebirdSql.Data.Client.Common.XsqldaMarshaler.MarshalManagedToNative(Charset charset, Descriptor descriptor) in c:\Users\Jiri\Documents\devel\NETProvider\working\NETProvider\source\FirebirdSql\Data\Client\Common\XsqldaMarshaler.cs:line 121
at FirebirdSql.Data.Client.Native.FesStatement.Execute() in c:\Users\Jiri\Documents\devel\NETProvider\working\NETProvider\source\FirebirdSql\Data\Client\Native\FesStatement.cs:line 355
at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteCommand(CommandBehavior behavior, Boolean returnsSet) in c:\Users\Jiri\Documents\devel\NETProvider\working\NETProvider\source\FirebirdSql\Data\FirebirdClient\FbCommand.cs:line 1246
at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteReader(CommandBehavior behavior) in c:\Users\Jiri\Documents\devel\NETProvider\working\NETProvider\source\FirebirdSql\Data\FirebirdClient\FbCommand.cs:line 566
at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteDbDataReader(CommandBehavior behavior) in c:\Users\Jiri\Documents\devel\NETProvider\working\NETProvider\source\FirebirdSql\Data\FirebirdClient\FbCommand.cs:line 666
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

堆栈跟踪指向 Firebird 库 (right here)。我尝试向后跟踪代码,但我不知道是为所有字段调用GetBytes() 还是只为byte[] 字段调用。 (我一开始以为可能和数据库中的__MigrationHistory.Model字段有关,但是如果那个表是空的,还是会报错。不过,我不希望我的推测造成误导。)

我可以解决这个问题,但我真的很想了解它。有谁知道这里发生了什么?

【问题讨论】:

  • 能否包含异常的堆栈跟踪?
  • @MarkRotteveel 我在帖子中添加了堆栈跟踪和一些其他信息。

标签: c# ef-code-first firebird entity-framework-6 firebird-embedded


【解决方案1】:

我遇到了同样的问题,实体初始化程序被嵌入了 Firebird 的错误:

Database.SetInitializer<FirebirdDbContext>(new CreateDatabaseIfNotExists<FirebirdDbContext>()); 

是问题,改成:

Database.SetInitializer<FirebirdDbContext>(null);

但它不会为您创建数据库。您可以检查数据库文件是否存在,然后更改初始化程序。

或者你可以创建你的初始化器来做同样的事情,并且可以工作:

public class MyCreateDatabaseIfNotExists : IDatabaseInitializer<FirebirdDbContext>
{
    public void InitializeDatabase(FirebirdDbContext context)
    {
        if (!context.Database.Exists())
        {
            context.Database.Create();
        }
    }
}

【讨论】:

  • 我为我的解决方法做了类似的事情。初始化现在似乎与 Entity Framework 6.1 一起正常工作,但现在当我使用 Linq 使用参数查询数据时,我遇到了同样的错误。不确定这是否是同一个问题,或者我是否应该提出一个新问题(或直接将其发送给 Firebird 开发人员)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2017-05-28
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多