简体   繁体   English

EF 6自动生成的数据库模型不适用于Web API 2

[英]EF 6 auto-generated db Model not working with Web API 2

I have a problem with generating a Web API 2. I'm creating a web API 2 for an existing database, the problem is I don't know fix the following problem: I'm creating a controller for my Sales table(but it has multiple foreign keys): 我在生成Web API 2时遇到问题。我正在为现有数据库创建Web API 2,问题是我不知道解决以下问题:我在为Sales表创建一个控制器(但是有多个外键):

Table model for FactSale: FactSale的表模型:

    public partial class FactSale
{
    public int FactSalesKey { get; set; }
    public Nullable<int> Quantity { get; set; }
    public decimal Amount { get; set; }
    public Nullable<decimal> AmountForeignCurrency { get; set; }

    public virtual DimCategory DimCategory { get; set; }
    public virtual DimDate DimDate { get; set; }
    public virtual DimDistributor DimDistributor { get; set; }
    public virtual DimRepresentative DimRepresentative { get; set; }
    public virtual DimUser DimUser { get; set; }
}

My auto-generated SalesContoller: 我自动生成的SalesContoller:

    public class SalesController : ApiController
    {
    private UniscoreDB db = new UniscoreDB();


    // GET api/Sales
    public IQueryable<FactSale> GetFactSales()
    {
        return db.FactSales;
    }
    }

This gives the following error: Type System.Data.Entity.DynamicProxies.FactSale_F6D0DDE3C293539DB57038CFC017F6A6566DEBB69E0AEA2C55DEC5761356A5C3' with data contract name 'FactSale_F6D0DDE3C293539DB57038CFC017F6A6566DEBB69E0AEA2C55DEC5761356A5C3:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. 这会产生以下错误: Type System.Data.Entity.DynamicProxies.FactSale_F6D0DDE3C293539DB57038CFC017F6A6566DEBB69E0AEA2C55DEC5761356A5C3' with data contract name 'FactSale_F6D0DDE3C293539DB57038CFC017F6A6566DEBB69E0AEA2C55DEC5761356A5C3:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. Type System.Data.Entity.DynamicProxies.FactSale_F6D0DDE3C293539DB57038CFC017F6A6566DEBB69E0AEA2C55DEC5761356A5C3' with data contract name 'FactSale_F6D0DDE3C293539DB57038CFC017F6A6566DEBB69E0AEA2C55DEC5761356A5C3:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I fixed this error by adding the following in my Context: base.Configuration.ProxyCreationEnabled = false; 我通过在上下文中添加以下内容来修复此错误: base.Configuration.ProxyCreationEnabled = false;

The problem then becomes the that, for example, DimCategory values are not visible in my JSON string. 然后问题就变成了例如DimCategory值在我的JSON字符串中不可见。 Now I'm searching for an easy solution to still get the proxy of my table in the JSON, or a better solution then disabling the ProxyCreationEnabled. 现在,我正在寻找一个简单的解决方案,以便仍在JSON中获取表的代理,或者是一个更好的解决方案,然后禁用ProxyCreationEnabled。

Thanks, Phoenix 谢谢凤凰

In the case you might want to use OData you should stick to the following: 如果您想使用OData,则应遵循以下规定:

I think what you actually want is to use expanding to tell the service you actually want to load those navigation properties too. 我认为您真正想要的是使用扩展来告诉服务您实际上也想要加载这些导航属性。

You can read about that here: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/using-$select,-$expand,-and-$value 您可以在此处阅读有关内容: http : //www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/using-$select,-$expand,-and-$value

In your particular case, that would just come to add a [Queryable] attribute on the collection getter and modifying your query to (for example): 在您的特定情况下,这只是在集合getter上添加[Queryable]属性并将查询修改为(例如):

GET http://localhost/api/Sales?$expand=DimCategory,DimDate

If you would like to stick with the "standard" web api 2 controller ApiController then I belive you will need to specify the properties to include in the code: 如果您想使用“标准” Web api 2控制器ApiController那么我相信您需要指定要包含在代码中的属性:

// GET api/Sales
public IQueryable<FactSale> GetFactSales()
{
    return db.FactSales
             .Include(x => x.DimCategory)
             .Include(x => x.DimDate)
             // ...and more if you need so...
             .AsQueryable();
}

I'd really recomment switching to OData ( http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint ) if you can afford to! 我真的建议切换到OData( http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint ),如果您负担得起! It's well tought and quite clean, and you get a tremendous amount of client-side control! 它非常坚固而且很干净,您可以获得大量的客户端控制!

Without seeing your full schema it is hard to say exactly, but a common reason for this error is "Loop Handling". 没有看到完整的架构,很难准确地说出,但是导致此错误的常见原因是“循环处理”。 Essentially, if you have an object which references another object which in turn references the first object again, it can create a loop that the JSON serializer cannot parse. 本质上,如果您有一个引用另一个对象的对象,而该对象又又引用了第一个对象,则它可以创建JSON序列化程序无法解析的循环。

Try adding GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 尝试添加GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; to your Global.asax. 到您的Global.asax。

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

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