简体   繁体   English

EF代码首先向模型和实体添加0到1的关系

[英]EF Code First add a 0 to 1 relation to Model and Entities

I'm trying to define the model 我正在尝试定义模型

 public class Refund
{
    [Key]   
    public int RefundID { get; set; }

    public int Order { get; set; }
    public int? TicketID { get; set; }
    public decimal? RefundTotal { get; set; }   
    public string RefundType { get; set; }
    public DateTime? RefundDate { get; set; }

    [ForeignKey("TicketID")]
    public virtual Ticket Ticket { get; set; }

}

My problem is that I would like to have access to the refund from the Ticket object, but the FK is on the refund Table. 我的问题是我想从票证对象获得退款,但是FK在退款表上。 Something like this : 像这样的东西:

Public class Ticket
{
    [Key]
    public int TicketID { get; set; }


    public virtual Refund Refund { get; set; }

}

I tried to define it with the .HasOptional() command from EF but can't make it work. 我尝试使用EF中的.HasOptional()命令定义它,但无法使其正常工作。

I don't really know if that is possible, any ideas? 我真的不知道这是否可行,有什么想法吗?

达到这种效果的方法将起作用:

Refund foundRefund = Refunds.SingleOrDefault(r => r.Ticket.Id == passedInTicketId);

I actually found the solution : 我实际上找到了解决方案:

Public class Ticket
{
    [Key]
    public int TicketID { get; set; }


    public virtual ICollection<Refund> Refund { get; set; }

}

As It is actually 0 to many relationship. 由于实际上是0对多的关系。

Sorry about that. 对于那个很抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM