2

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

Aqib Mehrban
  • 71
  • 2
  • 9
  • if You need to save your image in Database then you have to save the image as buffer byte array . But i suggest that save image in server and save the unique image name in database review http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0 – Sreerejith S S Oct 23 '15 at 09:44
  • Thank you , though this hasn't directly answered my question. – Aqib Mehrban Oct 23 '15 at 10:00

1 Answers1

0

Maybe I am missing something but I cant see where you set the user_no in your logic. Anyway, it seems there is no match between your user_no and existing users in your database to satisfy the foreign key. Make sure you set image's user_no to an existing user_no value in your user table.

You can just hardcode for test before your Add and Save calls:

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

But the final solution should get it either from current user or html form. Let's say something like:

pic.user_no = AuthenticationHelper.CurrentUser.UserID;

In both cases user row with user_no = 6 must exist in the table before.

BTW. I really don't like the naming convention you use... :P

mikus
  • 3,042
  • 1
  • 30
  • 40
  • Based on the controller when saving the image , how do I provide a value to the user_no? e.g. save image to tbl_picture where FK_user_no is = 6? Thank you – Aqib Mehrban Oct 23 '15 at 10:17
  • The issue is pic.user_no isn't recognized as a foreign key , the entity data model diagram doesn't map the foriegn key as it is. – Aqib Mehrban Oct 23 '15 at 10:30
  • ehh, so your problem is not with saving the data, it's with mapping the db to EntityFramework model? Can you show us the code of your tbl_picture class? it does not have user_no property? If so, just update EF model, there are tons of tutorials on this topic – mikus Oct 23 '15 at 11:19
  • Sure the class is public partial class tbl_picture with public int user_no , public string picture_content and public virtual tbl_user tbl_user {get ; set;} which meant to be the foreign key which contains user_no? – Aqib Mehrban Oct 23 '15 at 12:01
  • When setting to pic.tbl_user.user_no = 6; , it says object reference not set to an instance of an object? – Aqib Mehrban Oct 23 '15 at 12:06
  • pic.user_no = 6; not pic.tbl_user.user_no = 6, did you read my answer at all? Have you read any entity framework turorial on foreign keys? – mikus Oct 23 '15 at 12:37