I currently have two tables which one is connected with a foreign key :
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
Models have been generated using Entity Data Model Wizard.
I attempting to save an image within tbl.picture > picture content where user no has the value of 6 .
Controller
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);
}
Razor View (Index)
@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>
}
The issue I recieve is : Entities in UserDBContext.tbl_picture participate in the 'FK_tbl_picture_emp_no' relationship . 0 related 'tbl_user' were found , 1 'tbl_user' is expected.
I am attempting to save and upload an image in the same view according to a specific user_no , so in future when this user logs in , the profile picture will be displayed according to his user_no.
Thank you