3

I need to create a table and use FileStream in that table. In the SQL Server table, I need to use this column:

CREATE TABLE TestTable
(
    FileID  UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL UNIQUE DEFAULT(NEWID()),
    Pic     VARBINARY(MAX) FILESTREAM NULL
)

Now in EF Core code-first how can I create this column?

5
  • This is a question about generating the column, right? Not using the column? Commented Sep 18, 2019 at 19:47
  • @gunr2171 i need to add tow colmun in model with codefirst Commented Sep 18, 2019 at 19:48
  • Repeating what you said in your post when I ask a clarification question doesn't answer the question. Commented Sep 18, 2019 at 19:49
  • This is an answer from a previous version of EF. I doubt the functionally has changed. Commented Sep 18, 2019 at 19:50
  • @gunr2171 generation Commented Sep 18, 2019 at 19:50

2 Answers 2

0

See this solution, in which the author shows how to modify the Up() and Down() methods in an EF migration (i.e. the initial migration) to add a FILESTREAM column, and also how to perform the read/write operations.

He's using EF6, but I would think that the same could be done with EF Core.

Sign up to request clarification or add additional context in comments.

Comments

-1

In EF core , you could not use FileStream to save file to database.

Instead, you need to convert the file to byte[](which will convert to varbinary(max) in sql server) and copy the file content over when uploading using a memory-stream for instance.

Model:

public byte[] Picture { get; set; }

Convert file to byte array:

using (var ms = new MemoryStream())
{
  file.CopyTo(ms);
  byte[] fileBytes = ms.ToArray();
  string s = Convert.ToBase64String(fileBytes);
  // act on the Base64 data
}

Refer to

Store files in database using Entity Framework Core

How to convert a file into byte array in memory?

2 Comments

so from your answer it means that EF Core cannot be used to store a file as a Filestream but instead only as varbinary?
Response was not on the subject it was regarding mssql FileStream, not .net FileStream

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.