【问题标题】:How can i get a entity with sub entities in Entity framework?如何在实体框架中获取具有子实体的实体?
【发布时间】:2014-05-12 08:48:56
【问题描述】:

我有一个关于如何填充 json 对象以显示具有子实体的实体的问题。那是因为我有 x 有详细实体,称为 y。

DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("filtered_customer");

// 客户定义。 DbSyncTableDescription 客户描述 = SqlSyncDescriptionBuilder.GetDescriptionForTable("Sales.Customer", serverConn);

scopeDesc.Tables.Add(customerDescription);

// CustomerContact 的定义,包括要包含的列的列表。 集合 columnsToInclude = new Collection(); columnsToInclude.Add("CustomerId"); columnsToInclude.Add("PhoneType"); DbSyncTableDescription customerContactDescription = SqlSyncDescriptionBuilder.GetDescriptionForTable("Sales.CustomerContact", columnsToInclude, serverConn);

scopeDesc.Tables.Add(customerContactDescription);

【问题讨论】:

  • 你能展示你的FavoriteStation映射吗?
  • @VladimirSachek:我添加了我的映射。谢谢你的帮助....
  • 这还不够。你有像 HasRequired(t => t.Station).WithMany(t => t.FavoriteStations).HasForeignKey(d => d.StationId); 这样的映射吗?
  • 延迟加载是否被禁用?
  • @BenitoBertoli 这与 LazyLoading 无关,因为应该使用 Include 方法

标签: c# entity-framework orm entity-framework-5 odata


【解决方案1】:

应该做两个额外的步骤:

  1. 添加 EnableQuery 属性,如果不是您使用的最新版本,则添加 Queryable:

    [启用查询] public static y x(Guid Id)

  2. 将 $expand 添加到查询 URL:

    /odata/2012-01/x(guid'xxxx')?$expand=t

【讨论】:

    猜你喜欢
    • 2013-04-15
    • 2014-11-28
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多