简体   繁体   English

使用实体框架的代码优先在PostgreSQL中存储图像

[英]Using Entity Framework's Code First to store an image in PostgreSQL

I need to store an image in my PostgreSQL database, which I have written with Entity Framework 6's Code First, and mapped it via Npgsql. 我需要将图像存储在用Entity Framework 6的Code First编写的PostgreSQL数据库中,并通过Npgsql对其进行映射。

Other questions asked on the topic suggests a byte[] . 关于该主题的其他问题建议使用byte[] However, PostgreSQL cannot use byte[] . 但是,PostgreSQL不能使用byte[] It can however use a type called bytea , which is a byte array . 但是,它可以使用一个称为bytea的类型,它是一个字节数组 But how do I get this type in C#? 但是,如何在C#中获得这种类型?

Edit: 编辑:

I made an assumption, that it was the byte[] which could not be created. 我假设,这是无法创建的byte[] I did a Update-Database -verbose and it gave me the following: 我做了一个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'.

The class I am trying to add: 我要添加的课程:

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
}

I am not entirely sure what type is not resolved by Npgsql 我不确定Npgsql无法解析哪种类型

Turns out this happens because Npgsql is not installed in the GAC. 原来发生这种情况是因为GAC中安装Npgsql。

This issue was solved by installing the latest version of Npgsql . 通过安装最新版本的Npgsql解决了此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首先使用Entity Framework 4,MySQL和代码存储字节数组? - Store byte array using Entity Framework 4, MySQL and code first? 如何首先使用 Entity Framework 6 代码将 object 作为字符串存储在数据库中 - How to store an object as a string in database using Entity Framework 6 code first 实体框架+ PostgreSQL代码优先 - Entity Framework + PostgreSQL code-first MVC4图像上传使用实体框架5代码优先 - MVC4 Image Upload Using Entity Framework 5 Code First Entity Framework 5 代码先添加图片 - Entity Framework 5 Code first adding an image 在实体框架中使用数据库优先实体和代码优先实体 - Using database first entity with code first entity in entity framework 实体框架中的NHibernate的SchemaUpdate(代码优先)? - NHibernate's SchemaUpdate in Entity Framework (code first)? 如何使用带有实体框架代码优先的 Postgresql 的串行字段 - How to use a serial field with Postgresql with Entity Framework Code First 如何首先使用DotConnect For PostgreSQL和实体框架代码 - How to use DotConnect For PostgreSQL and entity framework code first 使用EF6 Store Functions for Entity Framework代码优先,我可以返回自定义类型吗? - Using EF6 Store Functions for Entity Framework code-first, can I return a custom type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM