简体   繁体   English

实体框架相关实体,错误加载

[英]Entity Framework related entity, wrong loading

I'm facing a problem with EF's related entities. 我遇到了EF相关实体的问题。

I have the following code: 我有以下代码:

public class Customer
{
    public int Id { get; set; }
    [ForeignKey("Id")]
    public virtual Status Status { get; set; }
}

public class Status
{
    public int Id { get; set; }
    public string Description { get; set; }
}

When i get a Customer entity the Status attr is lazy loaded, so far so good. 当我获得一个Customer实体时,Status attr会被延迟加载,到目前为止一切顺利。 However, the status obj is wrong. 但是,状态obj错误。

For Example. 例如。 I have a customer related with status id 5; 我有一个客户,其状态ID为5; But when I got it the status id is 1. 但是当我得到它的时候,状态ID是1。

The entities above are just examples. 上面的实体仅是示例。 In the real ones, all the related fields behave the same. 在实际情况下,所有相关字段的行为都相同。

Any help will be great. 任何帮助都会很棒。

Thanks. 谢谢。

SOLVED 解决了

It was my mistake. 是我的错

I changed to this and it worked. 我改成了这个,它奏效了。

Thanks for help. 感谢帮助。

public class Customer
{
    public int Id { get; set; }
    public int StatusId { get; set; }
    [ForeignKey("StatusId")]
    public virtual Status Status { get; set; }
}

public class Status
{
    public int Id { get; set; }
    public string Description { get; set; }
}

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

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