Store files in database using Entity Framework Core

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 06:25:10

You can convert the file bytes to a byte array.

public byte[] Avatar { get; set; }

Examine the accepted answer in the analogous approach for EF6: Save and retrieve image (binary) from SQL Server using Entity Framework 6

I am assuming that you are trying to use the windows filestream for sql server, which is not yet supported by .NET Core. You have to store the file as a byte array as already said (which will convert to varbinary(max) in sql server) and copy the file content over when uploading using a memory-stream for instance.

By Ef core, you can't store file in your database until now, so you can:

  1. store the result of reading files as byte[] like this :
public byte[] Avatar { get; set; }
var avatar =  File.ReadAllBytes(filePath);

2.use your machine as a file server and store the path of your file in database :

public string Avatar { get; set; }

In my Opinion the second way is better and i always use this pattern but it depends on your machine's H.D.D and the amount of files you want to store.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!