我有一个在 EF 数据库第一个 edmx 中定义的模型。从那里我公开了一些表格和视图(主要是视图)。由于可以使用 OData 扩充 EF 模型,我如何将复杂类型的导航属性添加到另一个 EF 和 OData 公开类型?
目前我定义了一个部分类并使用它们添加属性和属性。但看起来也可以使用 OData 的模型构建器功能添加所需的属性,或者更好的是,首先使用ODataConventionModelBuilder
然后增加结果。唉,我无法从现有的 API 文档和我找到的示例中拼凑出一个工作示例。
这是代码
//This class is generated from a view by EF (edmx)...
public partial class AccountView
{
public System.Guid Id { get; set; }
public int CompanyId { get; set; }
}
//Here's augmenting the EF generated view with some additional data...
[MetadataType(typeof(AccounViewMetaData))]
public partial class AccounView
{
//This is added here explicitly. AccountView itself exposes just
//a naked key, CompanyId.
public virtual Company Company { get; set; }
//This is just in case...
public class AccounViewDomainMetaData
{
//This is to add a navigation property to the OData $metadata. How to do this
//in WebApiConfig? See as follows...
[ForeignKey("Company")]
public int CompanyId { get; set; }
}
}
//This is an EF generated class one from an edmx..-
public partial class Company
{
public Company() { }
public int CompanyID { get; set; }
public string Name { get; set; }
}
//How to add a navigation property from AccountView to Company so that it'd become
//possible to call http://example.com/Accounts?$expand=Company and http://example.com/Accounts(1)?$expand=Company ?
var builder = new ODataConventionModelBuilder();
var companySet = builder.EntitySet<Entities.Company>("Companies");
var accountSet = builder.EntitySet<Entities.AccountView>("Accounts");
accountSet.EntityType.HasKey(i => i.Id); //EF has hard time recognizing primary keys on database first views...
//How to hide this from the result if there's a way to create a ?$expand=Company navigation property?
//accountSet.EntityType.Ignore(i => i.CompanyId);
这与我关于 OData 和模型的其他问题有关。