【问题标题】:MVC Controller - Storing Images In Database With A Foreign KeyMVC 控制器 - 使用外键将图像存储在数据库中
【发布时间】:2016-01-22 19:12:30
【问题描述】:

我目前有两张表,其中一张用外键连接:

dbo.tbl_user              dbo.tbl_picture
--------                  -----------
PK user_no                PK picture_id
user_username             picture_content
user_password             FK user_no (relational to tbl.user PK)
user_emailaddress

已使用实体数据模型向导生成模型。

我试图将图像保存在 tbl.picture > 图片内容中,其中 user no 的值为 6 。

控制器

UserDBContext db  = new UserDBContext(); (Entity Framework)


public ActionResult Create(tbl_picture pic , HttpPostedFileBase file)
if (ModelState.IsValid)
        {
            if (file != null)
            {
                file.SaveAs(HttpContext.Server.MapPath("~/Images/")
                                                      + file.FileName);
                pic.picture_content = file.FileName;
            }
            db.tbl_picture.Add(pic);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(pic);
    }

剃刀视图(索引)

@Html.ActionLink("Create New", "Create")
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.picture_content)
}

 @using (Html.BeginForm("Create", "Home", null, FormMethod.Post,
                          new { enctype = "multipart/form-data" }))
 {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.picture_id)
        </div>
        <div class="editor-field">
            <input id="ImagePath" title="Upload a product image"
                   type="file" name="file" />
        </div>
        <p><input type="submit" value="Create" /></p>
 }

我收到的问题是: UserDBContext.tbl_picture 中的实体参与“FK_tbl_picture_emp_no”关系。找到 0 个相关的“tbl_user”,预计有 1 个“tbl_user”。

我正在尝试根据特定的 user_no 在同一个视图中保存和上传图像,因此将来当该用户登录时,将根据他的 user_no 显示个人资料图片。

谢谢

【问题讨论】:

  • 如果您需要将图像保存在数据库中,那么您必须将图像保存为缓冲区字节数组。但我建议将图像保存在服务器中并将唯一的图像名称保存在数据库审查stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0
  • 谢谢,虽然这并没有直接回答我的问题。

标签: c# entity-framework model-view-controller controller


【解决方案1】:

也许我遗漏了一些东西,但我看不到你在逻辑中设置 user_no 的位置。无论如何,您的 user_no 和数据库中的现有用户之间似乎没有匹配来满足外键。确保将图像的 user_no 设置为用户表中现有的 user_no 值。

您可以在调用 Add 和 Save 之前对测试进行硬编码:

pic.user_no = 6;
db.tbl_picture.Add(pic);
db.SaveChanges();

但最终的解决方案应该是从当前用户或 html 表单中获取。让我们这样说:

pic.user_no = AuthenticationHelper.CurrentUser.UserID;

在这两种情况下,user_no = 6 的用户行必须存在于表中。

顺便说一句。我真的不喜欢你使用的命名约定......:P

【讨论】:

  • 保存图片时基于控制器,如何给user_no赋值?例如将图像保存到 FK_user_no = 6 的 tbl_picture?谢谢
  • 问题是 pic.user_no 没有被识别为外键,实体数据模型图没有按原样映射外键。
  • ehh,所以您的问题不在于保存数据,而在于将数据库映射到 EntityFramework 模型?你能告诉我们你的 tbl_picture 类的代码吗?它没有 user_no 属性?如果是这样,只需更新 EF 模型,关于此主题的教程有很多
  • 确定类是 public 部分类 tbl_picture 与 public int user_no , public string picture_content 和 public virtual tbl_user tbl_user {get ; set;} 表示包含 user_no 的外键?
  • 当设置为 pic.tbl_user.user_no = 6; ,它说对象引用未设置为对象的实例?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 2023-04-09
  • 2019-03-01
  • 1970-01-01
  • 2016-03-17
  • 2012-10-17
相关资源
最近更新 更多