亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Entity Framework Core - ForeingKey 顯示空值

Entity Framework Core - ForeingKey 顯示空值

C#
白豬掌柜的 2022-11-13 15:53:14
我正在使用 ASP.NET Core 開發一個實體框架,我正在 SQL Express 2017 中構建一個數據庫,并使用 C# 構建一個 REST API。我有兩個模型(Usuario 和 Alimento):這是我的 Alimento 模型:public class Alimento{[Key][DatabaseGenerated(DatabaseGeneratedOption.None)]public int codAl { get; set; }[Required(ErrorMessage = "Se requiere de una denominacion")][StringLength(50, MinimumLength = 2)]public string Descripcion { get; set; }public string Cantidad { get; set; }[Required(ErrorMessage = "Calorias es imprescindible")][DataType(DataType.Currency)][Range(1, 999, ErrorMessage = "El rango debe estar entre 1 y 999")]public int Calorias { get; set; }    }這是我的 Usuario 模型:public class Usuario{public int Id { get; set; }public string email { get; set; }[Required(ErrorMessage = "Se requiere de una denominacion")][StringLength(50, MinimumLength = 2)]public string nombre { get; set; }public Alimento alimento { get; set; }}當我遷移解決方案并更新數據庫時,我可以在 USUARIO 表中看到屬性“alimento”,如“alimentocodAl”。我正在嘗試建模“食物(ALIMENTO)已被用戶(USUARIO)吃掉”我有一個控制器,我從特定用戶那里獲取所有條目,但是當我使用 API 時,我在 USUARIO 表中的 foreing 鍵( Usuario foreing key to Alimento by codAl) 顯示空值,例如:[{"id":1,"email":"mail","nombre":"nombre","alimento":null}, {"id":2,"email":"mail","nombre":"nombre","alimento":null}, {"id":3,"email":"mail","nombre":"nombre","alimento":null}]這是不正確的,因為我的表中的這個列中有值。執行控制器的代碼是:public IEnumerable<Usuario> GetInfoUsuario(string email){    var user = ctx.usuarios.ToList().Where(b => b.email == email);    return user;}我有什么問題嗎?這是針對使用在 Windows 7 中運行的 ASP.NET 和 Entity Framework Core 開發的 REST API。
查看完整描述

3 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

您僅從 Usuario數據庫中檢索。如果您想獲得任何其他信息,您需要使用Include.

代替線var user = ctx.usuarios.ToList().Where(b => b.email == email);

你需要有var user = ctx.usuarios.Where(b => b.email == email).Include(usr => usr.alimento).ToList();

附言

ToList()作為最后一個操作移動,因為它評估表達式?,F在它將在檢索數據之前過濾表,而不是下載所有用戶并在之后過濾。


查看完整回答
反對 回復 2022-11-13
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

嘗試使用此示例進行映射:


public class Usuario{

public int Id { get; set; }

public string email { get; set; }


[Required(ErrorMessage = "Se requiere de una denominacion")]

[StringLength(50, MinimumLength = 2)]

public string nombre { get; set; }


[ForeignKey("alimento")]

public int AlimentoId {get; set;}


public virtual Alimento alimento { get; set; }

}



public class Alimento{

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.None)]

public int codAl { get; set; }


[Required(ErrorMessage = "Se requiere de una denominacion")]

[StringLength(50, MinimumLength = 2)]

public string Descripcion { get; set; }


public string Cantidad { get; set; }


[Required(ErrorMessage = "Calorias es imprescindible")]

[DataType(DataType.Currency)]

[Range(1, 999, ErrorMessage = "El rango debe estar entre 1 y 999")]

public int Calorias { get; set; }


public virtual ICollection<Usario> Users {get; set;}

    }

應該使用上面的代碼創建正確的一對多映射。像這樣的東西應該從 Usario 表收到。


[{"id":1,"email":"mail","nombre":"nombre","AlimentoId":1}] 


查看完整回答
反對 回復 2022-11-13
?
森林海

TA貢獻2011條經驗 獲得超2個贊

您能否嘗試將 Alimanto 包含在以下內容中

ctx.usuarios.Include(b => b.alimento)


查看完整回答
反對 回復 2022-11-13
  • 3 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號