场景
我需要将媒体文件以VarBinary的形式存储在数据库中,并将图像以nvarchar或VarBinary的形式存储(未确定)。我正在使用MVC5和实体框架。我已经为数据库中的另一个表制作了一个CRUD控制器,该表不包含媒体文件或图像,并且可以正常工作。
我到目前为止所做的事情
我创建了一个类别控制器,它与控制器一样基本,因为该表的所有数据类型都是文本或数字。我已经为MediaFiles控制器和Image控制器复制了此设计逻辑,但是我缺乏使它适应处理转换此文件并将其存储在mediafiles数据库中的知识。
模型
using System.Web;
namespace MediaOrganiser.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Image
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Image()
{
MediaFiles = new HashSet<MediaFile>();
}
public long ImageID { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Required]
public string FilePath { get; set; }
public HttpPostedFileBase ImageFile { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MediaFile> MediaFiles { get; set; }
}
}
控制器
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MediaOrganiser.Models;
using System.ComponentModel.DataAnnotations.Schema;
namespace MediaOrganiser.Controllers
{
public class ImageController : Controller
{
IMediaRepository mediaRepository = null;
public ImageController(IMediaRepository mediaRepository)
{
this.mediaRepository = mediaRepository;
}
public ImageController()
: this(new SQLMediaRepository())
{
}
// GET: Image
public ActionResult Index()
{
return View();
}
// GET: Image/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Image/Create
public ActionResult Create()
{
Image image = new Image();
return View(image);
}
// POST: Image/Create
[HttpPost]
public ActionResult Create(Image im)
{
string fileName = Path.GetFileNameWithoutExtension(im.ImageFile.FileName);
string extension = Path.GetExtension(im.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
im.FilePath = "~/Images/" + fileName;
fileName = Path.Combine(Server.MapPath("~/Images/") + fileName);
im.ImageFile.SaveAs(fileName);
using (MediaEntities db = new MediaEntities())
{
db.Images.Add(im);
db.SaveChanges();
}
ModelState.Clear();
return View();
}
// GET: Image/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Image/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Image/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Image/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
创建视图
@model MediaOrganiser.Models.Image
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Image", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Image</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FilePath, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="ImageFile" required />
@Html.ValidationMessageFor(model => model.FilePath, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我想要的
当我尝试创建图像时,出现以下错误消息:
值不能为null。参数名称:entitySet
我希望能够在C#中创建CRUD方法以在数据库中存储以下内容:
名称-nvarchar(255)
FilePath-nvarchar(Max)
我已遵循本教程:https://www.youtube.com/watch?v=5L5W-AE-sEs
非常感谢您的帮助。
答案 0 :(得分:1)
您应将财产public HttpPostedFileBase ImageFile { get; set; }
标记为[NotMapped]
[NotMapped]
public HttpPostedFileBase ImageFile { get; set; }
与所有不应映射且在数据库表中不存在的属性相同。