EF Core 2.0关系(Null Comig)

时间:2017-11-16 16:14:37

标签: entity-framework asp.net-core asp.net-core-mvc entity-framework-core asp.net-core-2.0

我正在使用asp.net core 2.0和EF core 2.0。

我有3个DbSet类。

    public class Person
    {
        [Key]
        public int PersonId { get; set; }
        public string Name { get; set; }
     }



    public class Packet
    {
        [Key]
        public int PacketId { get; set; }
        public string Name{get;set;}
        public int Point{get;set;}

    }



    public class UserPacket
    {
       [Key]
        public int UserPacketId { get; set; }      
        public int ParentPersonId { get; set; }
        public Person Parent { get; set; }
        public int PacketId { get; set; }      
        public  Packet Packet { get; set; }
    }

并且

 modelBuilder.Entity<UserPacket>().HasOne<Person>(a => a.Parent).WithMany().HasForeignKey(a=>a.ParentPersonId);
            modelBuilder.Entity<UserPacket>().HasOne<Person>(a => a.Owner).WithMany().HasForeignKey(a=>a.OwnerPersonId);
            modelBuilder.Entity<UserPacket>().HasOne<Packet>(a => a.Packet).WithMany().HasForeignKey(b=>b.PacketId);

这是我的桌子设计(不包含更多细节。) var person = blabla .....

 var personPacket = _userPacketRepository.GetMany(a => a.OwnerPersonId == person.PersonId);
            double totalPacket = 0;
            foreach (var packet in personPacket)
            {
                totalPacket += packet.Packet.Point;
            }

我可以选择数据包,但是数据包.Packet为空。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试加入

_userPacketRepository.GetMany(a => a.OwnerPersonId == person.PersonId)
.Include(x => x.Packet);

您可以从Github wiki上阅读有关EF及其路线图和更改的更多信息,这非常有用 https://github.com/aspnet/EntityFrameworkCore/wiki