“HttpPostedFileBase”没有定义键。定义此的EntityType关键 [英] 'HttpPostedFileBase' has no key defined. Define the key for this EntityType

查看:191
本文介绍了“HttpPostedFileBase”没有定义键。定义此的EntityType关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序上传图像数据库
这是我的模型

I have create an application to upload image in database this is my model

[Table("ImageGallery")]
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 TPADB : DbContext
{
    public DbSet<ImageGallery> imagegallery { get; set; }
}

这是我的看法。

@using (Html.BeginForm("Upload", "ImageUP", 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>

}

这是我的控制器

[HttpGet]
    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 (TPADB db = new TPADB())
        {
            db.imagegallery.Add(IG);
            db.SaveChanges();
        }
        return View();
    }

但它得来的错误
模型生成过程中检测到一个或多个验证错误:

but it throughs an error that "One or more validation errors were detected during model generation:

TPA.Models.HttpPostedFileBase:的EntityType'HttpPostedFileBase没有定义键。定义此的EntityType的关键。
HttpPostedFileBases:的EntityType:EntitySet的'HttpPostedFileBases'是基于类型'HttpPostedFileBase'一个没有定义键

TPA.Models.HttpPostedFileBase: : EntityType 'HttpPostedFileBase' has no key defined. Define the key for this EntityType. HttpPostedFileBases: EntityType: EntitySet 'HttpPostedFileBases' is based on type 'HttpPostedFileBase' that has no keys defined."

推荐答案

想通了,做如下更改模型:

Figured it out, make the following changes to the model:

public partial class ImageGallery
    {
        [Key]
        public int ImageID { get; set; }
        public int ImageSize { get; set; }
        public string FileName { get; set; }
        public byte[] ImageData { get; set; }
        public string File 
        {
            get
            {
                string mimeType = "image/png";
                string base64 = Convert.ToBase64String(ImageData);
                return string.Format("data:{0},{1}", mimeType, base64);
            }
        }

    }

那么这行添加到控制器:

Then add this line to the controller:

         HttpPostedFileBase File = Request.Files[0];

与文件例如更换任何IG.File条目:

Replace any IG.File entry with File for example:

         if (File.ContentLength > (2 * 1024 * 1024))

这篇关于“HttpPostedFileBase”没有定义键。定义此的EntityType关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆