简体   繁体   中英

Entity Framework related entity, wrong loading

I'm facing a problem with EF's related entities.

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. However, the status obj is wrong.

For Example. I have a customer related with status id 5; But when I got it the status id is 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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