【问题标题】:Relationships in POCO entitiesPOCO 实体中的关系
【发布时间】:2012-06-21 13:19:46
【问题描述】:

我刚刚为我的项目设置了 EF4Templates 并生成了 POCO 实体。在生成 POCO 实体之前,我使用的是普通的 EF 实体。我是 POCO 实体的新手,我确信我在某处错过了一步。我的 POCO 实体没有获取我的数据库模型的关系。当我使用 EF 模型时,它们运行良好。例如,我有两个名为 Appointments 和 Services 的表。在使用 POCO 实体之前,我可以像这样访问约会服务:

var svc = appointment.Service;

但现在它使用 POCO 返回 null。我究竟做错了什么?将不胜感激一些帮助。

非常感谢。

【问题讨论】:

    标签: c# asp.net-mvc-3 entity-framework poco


    【解决方案1】:

    您需要有一个代理跟踪导航属性。如果导航属性声明为public virtural,则会自动完成。

    class Dog{
      public int Id {get; set;} //CF will automatically recognise this as a primary key
      public virtual <List>Leg Legs{get; set;} //automagical navigation property
    }
    
    class Leg{
      public int Id {get; set;} //idem
      public virtual Dog PartOf {get; set;} //automagical navigation property
    }
    

    【讨论】:

    • 您能否详细说明如何将导航属性声明为公共虚拟?抱歉,我对 POCO 实体很陌生。
    • 好吧,就这样 ;) 我将把答案编辑得更详细一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多