【问题标题】:Using Entity Framework's Code First to store an image in PostgreSQL使用 Entity Framework 的 Code First 在 PostgreSQL 中存储图像
【发布时间】:2016-08-04 07:21:17
【问题描述】:

我需要在我使用 Entity Framework 6 的 Code First 编写的 PostgreSQL 数据库中存储一个图像,并通过 Npgsql 对其进行映射。

Other questions asked 在该主题上建议byte[]。但是,PostgreSQL 不能使用byte[]。但是,它可以使用称为bytea 的类型,即byte array。但是如何在 C# 中获得这种类型?

编辑:

我做了一个假设,它是无法创建的 byte[]。我做了一个Update-Database -verbose,它给了我以下信息:

PM> Update-Database -verbose
Using StartUp project 'Core.Shell'.
Using NuGet project 'Core.Database'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'TrackerDB' (DataSource: tcp://localhost:5432, Provider: Npgsql, Origin: Configuration).
Applying explicit migrations: [201604130907499_initial].
Applying explicit migration: 201604130907499_initial.
CREATE TABLE "public"."EmployeePhotoes"("EmployeePhotoId" int4 NOT NULL DEFAULT 0,"Image" bytea,"EmployeeId" int4 NOT NULL DEFAULT 0,CONSTRAINT "PK_public.EmployeePhotoes" PRIMARY KEY ("EmployeePhotoId"))
CREATE INDEX "EmployeePhotoes_IX_EmployeePhotoId" ON "public"."EmployeePhotoes" ("EmployeePhotoId")
ALTER TABLE "public"."EmployeePhotoes" ADD CONSTRAINT "FK_public.EmployeePhotoes_public.Employees_EmployeePhotoId" FOREIGN KEY ("EmployeePhotoId") REFERENCES "public"."Employees" ("EmployeeId")
ALTER TABLE "dbo"."__MigrationHistory" SET SCHEMA public
System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Npgsql.NpgsqlException,Npgsql, Version=3.0.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
    at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
    at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
    at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
    at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Type is not resolved for member 'Npgsql.NpgsqlException,Npgsql, Version=3.0.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.

我要添加的类:

public class EmployeePhoto
{
    //Primary key
    public int EmployeePhotoId { get; set; }

    public byte[] Image { get; set; }  

    //Foreign key
    public int EmployeeId { get; set; }

    //Navigation property
    public Employee Employee { get; set; } //1 photo for 1 Employee
}

我不完全确定 Npgsql 没有解析什么类型

【问题讨论】:

    标签: c# entity-framework postgresql ef-code-first npgsql


    【解决方案1】:

    Turns out 这是因为 Npgsql 没有安装在 GAC 中。

    通过安装最新版本的Npgsql 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 2011-06-18
      • 1970-01-01
      • 2011-06-27
      • 2012-05-15
      • 2015-10-22
      相关资源
      最近更新 更多