连接到其他模型的模型无法加载外键到代码 [英] Model connected to other models cannot load foreign keys to code

查看:29
本文介绍了连接到其他模型的模型无法加载外键到代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下模型类(预订)包含作为 ID 加载到数据库中的客户和露营地模型.但是,当我尝试检索所有这些预订的列表时,我无法访问与预订相关联的客户或营地信息.

The following model class(reservations) contains a customer and campingspot model which are loaded into the database as IDs. However when I try to retrieve a list of all these reservations I cannot acccess the customer or campspot information linked to the reservation.

public class Reservation
{


    [Key]
    public int ID { get; set; }

    [Required]
    public UserAccount Customer { get; set; }

    [Required]
    public CampingSpot CampSpot { get; set; }

我如何才能访问此信息,因为目前它始终是空值而不是 ID.

How would I make it possible to be able to access this information, because currently it is always a null value instead of an ID.

推荐答案

你想要的是

[RequiredNotDefault]
public int UserAccountId { get; set;}

[ForeignKey("UserAccountId")]
public virtual UserAccount Customer { get; set; }

CampingSpot 也一样

and the same for CampingSpot

这篇关于连接到其他模型的模型无法加载外键到代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆