C# “HttpPostedFileBase”未定义键
我有一个web应用程序将图像上传到数据库并检索它们C# “HttpPostedFileBase”未定义键,c#,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net Mvc,Entity Framework,Ef Code First,我有一个web应用程序将图像上传到数据库并检索它们 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=
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; }
}
现在,当我试图上传图像,它给我以下错误
EntityType“HttpPostedFileBase”未定义键。定义此EntityType的键。HttpPostedFileBase:EntityType:EntitySet“HttpPostedFileBase”基于未定义键的类型“HttpPostedFileBase”
我已经看到了关于堆栈溢出的一个问题--。我尝试了这个解决办法,但没有成功
我关注这个博客就是为了这个目的---
不能将HttpPostedFileBase存储在包含多个属性的复杂对象的数据库字段中。您可以使用[NotMapped]属性排除此项,但是您的模型和视图实际上没有关系,不包括模型其他属性的任何输入
相反,你的观点可以是公正的
@using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type = "submit" value="Upload" />
}
不能将HttpPostedFileBase存储在包含多个属性的复杂对象的数据库字段中。您可以使用[NotMapped]属性排除此项,但是您的模型和视图实际上没有关系,不包括模型其他属性的任何输入
相反,你的观点可以是公正的
@using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type = "submit" value="Upload" />
}
只需尝试具有如下属性的新视图模型---
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>
}
然后像这样更换控制器---
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>
}
}
然后像这样更改您的查看页面---
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>
}
只需尝试具有如下属性的新视图模型---
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>
}
然后像这样更换控制器---
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>
}
}
然后像这样更改您的查看页面---
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>
}
试试这个
[NotMapped]
public HttpPostedFileBase File { get; set; }
这不会映射到数据库
出现此错误是因为它们的表中没有数据类型HttpPostedFileBase
[NotMapped]
public HttpPostedFileBase File { get; set; }
这不会映射到数据库
发生此错误的原因是表中没有数据类型HttpPostedFileBase您无法在数据库中存储HttpPostedFileBase。将其从数据模型中删除,并使用包含该属性的视图模型。@StephenMuecke然后我将不得不进行许多更改。是否可以仅在数据模型和控制器中进行一些更改以使其工作?是否需要在视图中显示/编辑ImageGallery的任何属性?如果没有,那么只需使用public ActionResult UploadHttpPostedFileBase文件,并在视图中从数据模型中删除该属性,它仍然没有完全意义……如果我删除该属性,然后像这样尝试--public ActionResult UploadImageGallery IG,HttpPostedFileBase文件{file.FileName=IG.FileName;在这里我无法设置要保存到db中的文件名和内容长度属性。我在这里没有意义。请以其他方式查看代码:-IG.FileName=file.FileName;无法在数据库中存储HttpPostedFileBase。请将其从数据模型中删除并使用包含该属性的视图模型。@StephenMuecke然后我将不得不进行许多更改。是否可以仅在数据模型和控制器中进行一些更改以使其正常工作?是否需要在视图中显示/编辑ImageGallery的任何属性?如果不需要,则只需在视图中使用public ActionResult UploadHttpPostedFileBase文件和从数据模型中删除该属性仍然没有完全意义…如果我删除该属性,然后像这样尝试--public ActionResult UploadImageGallery IG,HttpPostedFileBase文件{file.FileName=IG.FileName;在这里,我无法设置要保存到db中的文件名和内容长度属性。我在这里没有意义。请以其他方式查看代码:-IG.FileName=file.FileName;您要求我添加一个答案,我已经完成了,您接受另一个答案,其中just剽窃了我的。祝你以后好运。你让我添加一个答案,我已经做了,你接受了另一个剽窃我的答案。祝你以后好运得到任何帮助。