Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to grab a property object from DB
    text
    copied!<p>I'm having some doubts, maybe newbie doubts but I just got into ASP.NET MVC 4 Basically I would like to know the correct way of grabbing details of an Object inside a model. In this case Image inside Contractor.</p> <p>Model:</p> <pre><code>public class Contractor { [Key] public int ContractorID { get; set; } [Required] public string Name { get; set; } [Required] public string Address { get; set; } public Image Avatar { get; set; } } public class Image { [Key] public int ImageID { get; set; } [Required] public string File_name { get; set; } [Required] public byte[] File_data { get; set; } } public class DATACRUD : DbContext { public DbSet&lt;Contractor&gt; Companies { get; set; } public DbSet&lt;Image&gt; Images { get; set; } } </code></pre> <p>Controller:</p> <pre><code>private DATACRUD db = new DATACRUD(); public ActionResult GetContractorAvatar(int id) { Contractor contractor = db.Contractors.Find(id); if (contractor == null) { return HttpNotFound(); } Image avatar = contractor.Avatar; </code></pre> <hr> <p>Problem 1)</p> <p>avatar == null, but is not suppose to be because when I created the object Contractor, I added the image sucessfully (I checked in the DB and it is there)</p> <p>The solution I'm seeing is instead of having Image property in Contractor.cs model, I would just put a string property with the image key.</p> <hr> <p>Problem 2)</p> <p>Even If could grab the image key like I said in the previous problem, when I pass my mouse in Debug mode over private DATACRUD db = new DATACRUD ();</p> <p>db.Images is also empty...</p> <hr> <pre><code> return File(avatar.File_data, "image"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload