您好,我正在嘗試添加一個模型,可以將文件存儲到現有的ASP.NET MVC Code First項目中。包含文件的ASP.NET MVC模型
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.
我錯過了什麼?
而不是在實體模型中有'HttpPostedFileBase',我建議您將File存儲爲'byte []'在實體模型中(並反過來在數據庫中),並創建另一個模型視圖。 – Paritosh
'HttpPostedFileBase'不代表一個實際的文件,它代表一個文件流。您無法直接序列化文件流。它必須首先被「下載」(或者至少它的一部分,記住一個文件可能是千兆字節,因此你不會直接把它加載到內存中)。 –
謝謝,您的意見讓我走上了正軌,之後我設法找到了一些也幫助我的相關帖子。希望他們將在未來用於其他人:http://stackoverflow.com/questions/7852102/convert-httppostedfilebase-to-byte,http://stackoverflow.com/questions/15106190/uploading-files -into-database-with-asp-net-mvc – Chopo87