Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store files in database using Entity Framework Core

How to store files in a SQL Server database using Entity Framework Core (Code-First) in an ASP.NET Core app?

I have tried using Filestream but unfortunately I get this error:

Cannot call Property for the property 'Avatar' on entity type 'UserProfile' because it is configured as a navigation property. Property can only be used to configure scalar properties

Here's the code:

public class UserProfile : BaseEntity {
    public FileStream Avatar { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Sex Sex { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }

    public virtual IEnumerable<Email> Emails { get; set; }
    public virtual User User { get; set; }
    public int UserID { get; set; }
}

And mapping:

public class UserProfileMap {
    public UserProfileMap(EntityTypeBuilder<UserProfile> entityBuilder) {
        entityBuilder.HasKey(e => e.ID);
        entityBuilder.Property(e => e.Avatar);
        entityBuilder.Property(e => e.FirstName);
        entityBuilder.Property(e => e.LastName);
        entityBuilder.Property(e => e.DateOfBirth);
        entityBuilder.Property(e => e.Sex);
        entityBuilder.Property(e => e.Address);
        entityBuilder.Property(e => e.PhoneNumber);
        entityBuilder.HasMany(e => e.Emails).WithOne(u => u.UserProfile).HasForeignKey(x => x.UserProfileID);
    }
}

What do I do? Thanks!

like image 498
stroibot Avatar asked Feb 17 '18 19:02

stroibot


People also ask

Is it possible to store files in Entity Framework?

The "right" way to store a file in a SQL Server 2008 database is to use the FILESTREAM data type. I'm not aware that the Entity Framework supports that, but you can certainly try and see what happens. That said, most of the time when people do this, they don't store the file in the database.

Why use Entity Framework for database management?

A solution that uses the Entity Framework to store files in the Database A solution that detects and prevents from uploading the same file twice via some kind of hash/checksum Tips on database/table design

How to store file name and path into database table using entity?

Hi, In this blog I am writing sample code to store file name and path into database table using Entity framework code first approach. Step: 1 First Create Controller named Home Controller . Step 2: Create Index.cshtml view into Index Action of Home controller. Step 3: Write following code into Index.cshtml

Can I attach the demo project without package for Entity Framework?

I did attach the demo project without package for Entity Framework 6.0 due to file size exceeded 25MB. In this article, we learned how to work with strongly-typed View to upload a file, validate the file, and store it to a physical path as well as a database table.


3 Answers

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

like image 121
Roman Bartke Avatar answered Oct 16 '22 17:10

Roman Bartke


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.

like image 3
alsami Avatar answered Oct 16 '22 17:10

alsami


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.

like image 1
farid salehi Avatar answered Oct 16 '22 17:10

farid salehi