包含File的ASP.NET MVC模型

Cho*_*o87 3 asp.net-mvc entity-framework ef-code-first ef-migrations

您好我正在尝试添加一个可以将文件存储到现有ASP.NET MVC Code First项目的模型.

namespace MyMVCApp.Models
{
  public class FileUpload
  {
    [Key]
    public int ID { get; set; }
    public string FileName { get; set; }
    public string FilePath { get; set; }

    [Required]
    public HttpPostedFileBase File { get; set; }
  }
}
Run Code Online (Sandbox Code Playgroud)

我已将以下内容添加到项目的DbContext文件中:

public DbSet<FileUpload> FileUploads { get; set; }
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用Package Manager控制台中的Entity-Framework Migrations将表添加到现有数据库:

PM> Add-Migration FileUpload
Run Code Online (Sandbox Code Playgroud)

在运行ubove命令时,我收到以下错误:

System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'HttpPostedFileBase' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'HttpPostedFileBases' is based on type 'HttpPostedFileBase' that has no keys defined.

   at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)

   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'HttpPostedFileBase' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'HttpPostedFileBases' is based on type 'HttpPostedFileBase' that has no keys defined.
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Mar*_*oth 8

您无法使用实体框架保留任意.NET类.

有几个需求类必须满足,以便实体框架能够持久化它们,例如在错误中调用的那个(没有键定义)

实际上,您应该尝试仅保留原始类型(int,string等)和您自己的目的设计类.

在这种情况下,正确的类型是 byte[]

这意味着您需要排除在模型上具有属性HttpPostedFileBase.

相反,请考虑使用字节数组来存储数据,并使用另一个属性,例如用于存储文件类型/名称的字符串