【问题标题】:ASP.NET MVC Model containing File包含文件的 ASP.NET MVC 模型
【发布时间】:2013-06-21 01:07:48
【问题描述】:

您好,我正在尝试添加一个可以将文件存储到现有 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; }
  }
}

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

public DbSet<FileUpload> FileUploads { get; set; }

我正在尝试使用包管理器控制台中的实体框架迁移将表添加到现有数据库:

PM> Add-Migration FileUpload

运行 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.

我错过了什么?

【问题讨论】:

  • 我建议您将文件存储为 byte[] 在实体模型中(反过来,在 DB 中)并为视图创建另一个模型,而不是在实体模型中使用 HttpPostedFileBaseclass="comcopy">跨度>
  • HttpPostedFileBase 不代表一个实际的文件,它代表一个文件流。您不能直接序列化文件流。它必须首先被“下载”(或者至少是其中的一部分,记住一个文件可能有千兆字节长,因此您不希望将它直接加载到内存中)。
  • 谢谢您,您的 cmets 让我走上了正轨,之后我设法找到了一些相关的帖子,这些帖子也帮助了我。希望他们将来对其他人有用:stackoverflow.com/questions/7852102/…stackoverflow.com/questions/15106190/…

标签: asp.net-mvc entity-framework ef-code-first entity-framework-migrations


【解决方案1】:

您不能使用实体框架持久化任意 .NET 类。

必须满足几个要求类才能使实体框架能够持久化它们,例如在错误中调用的要求(未定义键)

确实,您应该尝试仅保留原始类型(int、string 等)和您自己专门设计的类。

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

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

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

【讨论】:

  • 好吧,如果我理解正确的话,与模型一起使用的正确变量是byte[] 数组,而不是HttpPostedFileBase 对象。
猜你喜欢
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多