3

Hello I am trying to add a model that can store a file to an existing ASP.NET MVC Code First project.

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; }
  }
}

I have added the following to the project's DbContext file:

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

I am trying to add the table to the existing database using Entity-Framework Migrations within the Package Manager Console:

PM> Add-Migration FileUpload

On running the ubove command I get the following error:

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.

What am I missing?

Chopo87
  • 1,240
  • 4
  • 19
  • 32
  • 4
    Instead of having `HttpPostedFileBase` in Entity Model, I suggest you to store File as `byte[]` in the Entity Model (and in turn, in DB) and create another Model for the view. – Paritosh Jun 24 '13 at 13:44
  • 2
    `HttpPostedFileBase` does not represent an actual file, it represents a file stream. You cannot serialize a file stream directly. It must first be "downloaded" (or at least parts of it, remember a file can be gigabytes long, and as such you wouldn't want to load it directly into memory). – Erik Funkenbusch Jun 24 '13 at 14:35
  • 1
    Thank you, your comments put me on the right track and after that I managed to find some related posts that also helped me out. In the hope that they will be of use to others in the future: http://stackoverflow.com/questions/7852102/convert-httppostedfilebase-to-byte, http://stackoverflow.com/questions/15106190/uploading-files-into-database-with-asp-net-mvc – Chopo87 Jun 24 '13 at 14:52

1 Answers1

8

You can't persist arbitrary .NET classes using the entity framework.

There are several requirements classes must satisfy in order for the entity framework to be able to persist them such as the one called out in the error (no key defined)

Really, you should be trying to persist primitive types (int, string, etc) and your own purpose designed classes only.

In this case the correct type is byte[]

That means you need to rule out having a property HttpPostedFileBase on your model.

Instead, consider using a byte array to store the data, and another property, e.g. string to store the filetype/name

Chopo87
  • 1,240
  • 4
  • 19
  • 32
Martin Booth
  • 8,485
  • 31
  • 31
  • Ok, so If I have understood correctly, the correct variable to use withing the model is a `byte[]` array, not infact a `HttpPostedFileBase` object. – Chopo87 Jun 24 '13 at 14:38