我一直試圖從已加入主用戶表的表中獲取數據,第二個表用於存放圖像。我現在的代碼發佈在下面,當我想要檢索ImagePath字段時,只從表中返回ImageID,只是爲了說明這是一個單獨的表格,因爲用戶可以添加許多圖像。加入表格並從中獲取數據
這些機型:
[Table("accountInfo")] // Table name
public class accountInfo
{
[Key]
public int AccountID { get; set; }
public int UserId { get; set; }
public int UserIdent { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<UserImages > UserImages { get; set; }
}
[Table("UserImages")] // Table name
public class UserImages
{
[Key]
public int ImageID { get; set; }
public int AccountID { get; set; }
public string ImagePath { get; set; }
public string ImageDesc { get; set; }
public int ProfileImage { get; set; }
}
控制器:
public ActionResult Index()
{
int id = (int)WebSecurity.CurrentUserId;
var users = db.AccountInformation.Include(c => c.UserImages).Where(c => c.UserId == id);
return View(users.ToList());
}
我假設我已經錯在建立模型。誰能幫忙?
是否有兩個表 - 「accountInfo」和「AccountInformation」? – siddharth
我使用了visual webdevelopers scaffolding,這就是放置它,它似乎直接與accountInfo關聯。 – user2029541