实体类型 'stream' 需要定义主键。 .Net 核心 2.1
The entity type 'stream' requires a primary key to be defined. .Net Core 2.1
我将 .NET Core 2.1.1 与 Entity Framework Core 2.1.1 一起使用,并且我有以下实体:
一些实体
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; }
}
}
它代表一个 Certificate 对象,我计划在其中存储 PDF 文件的 FileStream,当然使用最后一个 属性。但是,当我尝试使用 EF Core 的包管理器控制台命令 Add-Migration Foo
进行 运行 迁移时,或者当我尝试使用内存数据库 运行 项目时,出现以下错误:
When trying to add a Role
The entity type 'stream' requires a primary key to be defined.
只有在实体中存在最后一个 属性 (FileStream) 时才会发生,如果我将其删除,它会正常工作。我搜索了其他相关问题,大多数都指向:
- 由于命名约定,EF 无法识别主键。
- 必须使用 Fluent API.
显式定义组合键
- 只需在主键前放置一个
[Key]
属性 属性。
我还尝试使用 Fluent API:
定义主键
这是我的 DbContext:
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.
似乎是关于Stream
类型的使用,但我已经定义了一个主键。有什么建议吗?非常感谢您的帮助。
您无法在当前使用 EF Core 的数据库中存储文件 stream。您有几个选择:
存储读取该文件流的结果,在 PDF 文件的情况下将是 byte[]
blob。不过通常建议不要将文件存储在数据库中。
将路径存储到数据库中的 PDF 文件。例如"Documents/Certificates/xxxx.pdf"
如果您不需要将 PDF 保存在数据库中,那么只需告诉 EF 忽略它即可。这可以通过将 NotMapped
属性添加到 属性:
来完成
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; }
}
或流利 API:
builder.Entity<Certificate>()
.HasKey(c => c.Id)
.Ignore(c => c.FileStream);
我将 .NET Core 2.1.1 与 Entity Framework Core 2.1.1 一起使用,并且我有以下实体:
一些实体
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; }
}
}
它代表一个 Certificate 对象,我计划在其中存储 PDF 文件的 FileStream,当然使用最后一个 属性。但是,当我尝试使用 EF Core 的包管理器控制台命令 Add-Migration Foo
进行 运行 迁移时,或者当我尝试使用内存数据库 运行 项目时,出现以下错误:
When trying to add a Role
The entity type 'stream' requires a primary key to be defined.
只有在实体中存在最后一个 属性 (FileStream) 时才会发生,如果我将其删除,它会正常工作。我搜索了其他相关问题,大多数都指向:
- 由于命名约定,EF 无法识别主键。
- 必须使用 Fluent API. 显式定义组合键
- 只需在主键前放置一个
[Key]
属性 属性。
我还尝试使用 Fluent API:
定义主键这是我的 DbContext:
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.
似乎是关于Stream
类型的使用,但我已经定义了一个主键。有什么建议吗?非常感谢您的帮助。
您无法在当前使用 EF Core 的数据库中存储文件 stream。您有几个选择:
存储读取该文件流的结果,在 PDF 文件的情况下将是
byte[]
blob。不过通常建议不要将文件存储在数据库中。将路径存储到数据库中的 PDF 文件。例如
"Documents/Certificates/xxxx.pdf"
如果您不需要将 PDF 保存在数据库中,那么只需告诉 EF 忽略它即可。这可以通过将
来完成NotMapped
属性添加到 属性: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; } }
或流利 API:
builder.Entity<Certificate>() .HasKey(c => c.Id) .Ignore(c => c.FileStream);