【问题标题】:'HttpPostedFileBase' has no key defined'HttpPostedFileBase' 没有定义键
【发布时间】:2023-03-09 03:41:01
【问题描述】:

我有一个网络应用程序可以将图像上传到数据库并检索它们。

public class ImageGallery
{
    [Key]
    public int ImageID { get; set; }
    public int ImageSize { get; set; }
    public string FileName { get; set; }
    public byte[] ImageData { get; set; }
    [Required(ErrorMessage="Please select Image File")]
    public HttpPostedFileBase file { get; set; }
}

我的数据库上下文类是这样的

public class MyDatabaseEntities : DbContext
{
    public DbSet<ImageGallery> ImageGalleries { get; set; }
}

这是我的控制器

public ActionResult Upload()
{
    return View();
}
[HttpPost]
public ActionResult Upload(ImageGallery IG)
{
    IG.FileName = IG.File.FileName;
    IG.ImageSize = IG.File.ContentLength;
    byte[] data = new byte[IG.File.ContentLength];
    IG.File.InputStream.Read(data, 0, IG.File.ContentLength);
    IG.ImageData = data;
    using(MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        dc.ImageGalleries.Add(IG);
        dc.SaveChanges();
    }
    return RedirectToAction("Gallery");
}

现在当我尝试上传图片时,出现以下错误

EntityType 'HttpPostedFileBase' 没有定义键。定义此 EntityType 的键。 HttpPostedFileBases: EntityType: EntitySet 'HttpPostedFileBases' 基于没有定义键的类型'HttpPostedFileBase'。

我看到堆栈溢出问题之一-----'HttpPostedFileBase' has no key defined. Define the key for this EntityType。我尝试了解决方案,但没有成功。

我为此目的关注此博客------ http://dotnetawesome.com/mvc/how-to-upload-image-to-database-and-show-in-view-without-image-handler

【问题讨论】:

  • 您不能在数据库中存储HttpPostedFileBase。从您的数据模型中删除它并使用包含该属性的视图模型。
  • @StephenMuecke 那么我将不得不进行许多更改。是否可以仅在数据模型和控制器中进行一些更改以使其正常工作
  • 您需要在视图中显示/编辑ImageGallery 的任何属性吗?如果不是,那么只需使用public ActionResult Upload(HttpPostedFileBase file) 并在视图中使用&lt;input type="file" name="file" /&gt;(并从您的数据模型中删除该属性)
  • 它仍然没有完全意义....如果我删除该属性然后像这样尝试----- public ActionResult Upload(ImageGallery IG, HttpPostedFileBase file) { file.FileName = IG 。文件名;在这里我无法设置文件名和内容长度属性以保存到数据库中。我在这里没有意义。 PLzzz 看代码
  • 其他方式:) - IG.FileName = file.FileName;

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


【解决方案1】:

试试这个

[NotMapped]
public HttpPostedFileBase File { get; set; }

这不会映射到数据库。

发生错误是因为它们在您的表中没有数据类型HttpPostedFileBase

【讨论】:

    【解决方案2】:

    您不能将HttpPostedFileBase 存储在数据库字段中(它是一个包含多个属性的复杂对象)。您可以使用 [NotMapped] 属性排除它,但是您的模型和视图实际上没有任何关系(您不包括模型其他属性的任何输入)。

    相反,您的视图可以只是

    @using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="file" />
        <input type = "submit" value="Upload" />
    }
    

    并将 POST 方法更改为

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
        if (file.ContentLength > 0) // check a file was selected
        {
           // Initialize a new instance of the data model and set its properties
           ImageGallery model = new ImageGallery()
           {
               FileName = file.FileName,
               ImageSize = file.ContentLength,
               ....
           };
           .... // save and redirect
        }
        else {
          // add a model state error and return the view?
        }
    }
    

    【讨论】:

    • 你要求我添加一个答案,我已经完成了,你接受了另一个(这只是抄袭我的)。祝你以后能得到任何帮助。
    【解决方案3】:

    只需尝试具有此类属性的新视图模型-----

        public class ImageViewModel
       {
        public int ImageID { get; set; }
        public int ImageSize { get; set; }
        public string FileName { get; set; }
        public byte[] ImageData { get; set; }
        public HttpPostedFileBase File { get; set; }
       }
    

    并像这样更改您的控制器-----

        public ActionResult Upload(HttpPostedFileBase file)
        {
            ImageGallery IG = new ImageGallery();
        IG.FileName = file.FileName;
          IG.ImageSize = file.ContentLength;
    
            byte[] data = new byte[file.ContentLength];
            file.InputStream.Read(data, 0, file.ContentLength);
    
               IG.ImageData = data;
    
            var model = new ImageViewModel
            {
                FileName = file.FileName,
                ImageSize = file.ContentLength,
                ImageData = data,
                File = file
            };
    
    
            using(MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                dc.ImageGalleries.Add(IG);
                dc.SaveChanges();
            }
            return View(model);
    
        }
    
    }
    

    }

    并像这样更改您的视图页面------

        @model Image.Models.ImageViewModel
    
    <h2>Upload</h2>
     @using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
     {
      @Html.ValidationSummary(true)
      <table>
        <tr>
            <td>Select File : </td>
            <td>
                @Html.TextBoxFor(Model => Model.File, new { type = "file" })
                @Html.ValidationMessage("CustomError")
            </td>
           <td>
                <input type="submit" value="Upload" />
            </td>
        </tr>
    </table>
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2016-03-18
      • 1970-01-01
      • 2016-04-02
      • 2016-03-16
      相关资源
      最近更新 更多