简体   繁体   English

EF Code First - 使用外键选择

[英]EF Code First - Select with Foreign Key

Getting what I think is an odd behaviour in EF, which I'm hoping someone can shed some light on.在 EF 中得到我认为的一种奇怪行为,我希望有人能对此有所了解。 Basicaly, if I retrieve an item with a foreign key, the foreign key item isn't retrieved?基本上,如果我检索带有外键的项目,则不会检索外键项目? This seems like a bit of a short coming.这似乎有点短暂。 Have I missed something obvious or is there a pattern to deal with this?我是否遗漏了一些明显的东西,或者是否有解决这个问题的模式?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace EFTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new MCInitializer());

            EFTestEM context = new EFTestEM();
            var foos = from m in context.Foo
                    select m;

            foreach (var foo in foos)
            {
                // foo.MyBar is null?! How do I populate it?
                Console.WriteLine("{0},{1}",foo.Desc,foo.MyBar.Whatever);
            }

        }

    }


    [Table("tbl_Bar")]
    public class Bar
    {
        [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int BarId { get; set; }
        public string Whatever { get; set; }
        public string Whenever { get; set; }
    }


    [Table("tbl_Foo")]
    public class Foo
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int FooId { get; set; }
        public string Desc { get; set; }
        public int MyBarId { get; set; }
        [ForeignKey("MyBarId")]
        public Bar MyBar { get; set; }

    }

    public class MCInitializer : DropCreateDatabaseAlways<EFTestEM>
    {
        protected override void Seed(EFTestEM context)
        {
            List<Bar> bars = new List<Bar>
                             {
                                 new Bar(){Whatever = "Bar1"},
                                 new Bar(){Whatever = "Bar2"},
                                 new Bar(){Whatever = "Bar3"},
                             };


            List<Foo> foos = new List<Foo>
                             {
                                 new Foo() {Desc = "Foo1", MyBar = bars[0]},
                                 new Foo() {Desc = "Foo2", MyBar = bars[1]},
                                 new Foo() {Desc = "Foo3", MyBar = bars[2]}
                             };

            foreach (var bar in bars)
                context.Bar.Add(bar);
            foreach (var foo in foos)
                context.Foo.Add(foo);

            context.SaveChanges();

            base.Seed(context);

        }
    }
}

For lazy loading you need to make your related properties virtual.对于延迟加载,您需要将相关属性设为虚拟。

Eg例如

public virtual Bar MyBar { get; set; }

Related properties need to either be 'eagerly loaded' or 'lazy loaded' (see Charlino's answer).相关属性需要“急切加载”或“延迟加载”(参见 Charlino 的回答)。

To eagerly load, the code needs to make use of the 'Include' extension.:要急切加载,代码需要使用“包含”扩展名。:

        var foos = from m in context.Foo.Include("MyBar")
                select m;

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

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