I'm Using .NET Core 2.1.1 with Entity Framework Core 2.1.1, and I have the following Entity:
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
namespace MyWebApp.Models
{
public class Certificate
{
[Key]
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime RequestedAt { get; set; }
public Stream FileStream { get; set; }
}
}
Which represents a Certificate object where I plan to store the FileStream of a PDF file, using the last property of course. But When I try either to run a Migration using EF Core's Package Manager console command Add-Migration Foo
, or when I try to run the project using an in-memory DataBase, I get the following errors:
When trying to add a Role
The entity type 'stream' requires a primary key to be defined.
It only happens when the last property (FileStream) is present in the Entity, if I remove it works fine. I've searched other related questions, and most of them point to either:
[Key]
attribute before the primary key property.I've also tried to define the primary key using the Fluent API:
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.IO;
namespace MyWebbApp.Models
{
public class DbContext : IdentityDbContext<IdentityUser>
{
public DbSet<ActionValue> ActionValues { get; set; }
public DbSet<Certificate> Certificates { get; set; }
public DbSet<VisualIVR> VisualIVRs { get; set; }
public DbSet<SMSRequest> SMSRequests { get; set; }
public DbContext (DbContextOptions<VysContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Certificate>()
.HasKey(c => c.Id);
}
}
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at VysMiddleware.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) in H:/MyApp/Startup.cs:line 84
Inner Exception 1:
AggregateException: One or more errors occurred.
Inner Exception 2:
InvalidOperationException: The entity type 'Stream' requires a primary key to be defined.
It seems to be something regarding the use of the Stream
type, but I already have a primary key defined. Any suggestions?, Thanks so much for your help.
You can't store a file stream in your database currently with EF Core. You have a few options:
Store the result of reading that file stream, which in the case of PDF file would be a byte[]
blob. It's generally recommended not to store files in the database though.
Store the path to the PDF file in your database. e.g. "Documents/Certificates/xxxx.pdf"
If you don't need the PDF persisted in the database, then just tell EF to ignore it. This can be done by adding a NotMapped
attribute to the property:
public class Certificate
{
[Key]
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime RequestedAt { get; set; }
[NotMapped]
public Stream FileStream { get; set; }
}
or in Fluent API:
builder.Entity<Certificate>()
.HasKey(c => c.Id)
.Ignore(c => c.FileStream);
User contributions licensed under CC BY-SA 3.0