如何从一个EF模型ODataConventionModelBuilder建立了一个模型添加复杂性(H

2019-10-21 10:58发布

我有一个在EF数据库第一EDMX定义的模型。 从那里我揭露一些表和视图(主要意见)。 因为它可能增加与OData的生态足迹模型,我怎么可能一个复合型的导航属性添加到另一个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和模型。

文章来源: How to add complex properties on a model built with ODataConventionModelBuilder from an EF model