asp.net-mvc - EF oData - 元素类型不是实体类型

标签 asp.net-mvc entity-framework odata

我目前正在尝试启动并运行一个 odata 演示,但我看到了这个错误:

'On data context type 'TweetPicContext', there is a top IQueryable property 'Users' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.

令人讨厌的是,我的代码几乎是对现有项目的剪切和粘贴——但显然出了点问题......

查看以前的问题/答案,常见的答案似乎是 DataServiceKey 属性 - 但我已经在使用它了。

有人知道我哪里出错了吗?

这是我的数据上下文:

public class TweetPicContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Picture> Pictures { get; set; }
}

这是我的用户数据类:

[DataServiceKey("UserID")]
public class User
{
    [Key]
    public int UserID { get; set; }

    public string Name { get; set; }
    public string TwitterHandle { get; set; }
    public string TwitterIconUrl { get; set; }
    public int TwitterId { get; set; }
    public string TwitterAuthorizationToken { get; set; }
    public string TwitterAuthorizationTokenSecret { get; set; }
    public DateTime DateJoinedUtc { get; set; }
    public string UniqueDeviceToken { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; } 

    public User()
    {
        DateJoinedUtc = DateTime.UtcNow;
        UniqueDeviceToken = Guid.NewGuid().ToString("N");
    }
}

这是我的用户服务:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class TweetPicOData : DataService<TweetPicContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(IDataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Users", EntitySetRights.AllRead);
        config.SetEntitySetAccessRule("Pictures", EntitySetRights.AllRead);
        config.UseVerboseErrors = true;
    }

    // see http://romiller.com/2010/07/19/ef-ctp4-tips-tricks-wcf-data-service-on-dbcontext/
    protected override TweetPicContext CreateDataSource()
    {
        var ctx = base.CreateDataSource();

        // Disable proxy object creation.
        ctx.Configuration.ProxyCreationEnabled = false;
        return ctx;
    }
}

最佳答案

WCF 数据服务 5.0 添加了在使用 DbContext 时调用 EF 提供程序所需的支持。如果您将 DbContext 与早期版本的 WCF 数据服务一起使用,堆栈将回退到使用反射提供程序而不是 EF 提供程序。这将导致类似于您所描述的行为。

升级到 WCF 数据服务 5.0 或更高版本应该可以解决此问题。从 5.0 版开始,我们一直通过 NuGet 分发位:http://www.nuget.org/packages/Microsoft.Data.Services.Client .

HTH, 标记

关于asp.net-mvc - EF oData - 元素类型不是实体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103708/

相关文章:

c# - 在没有 "save as.."的情况下从 asp.net 本地保存文件? ActiveX 是唯一的解决方案吗?

asp.net-mvc - ASP.Net MVC 3 哪些文件夹是网络可见的

c# - 如何使用对象上下文在 Entity Framework 中使用批量插入?

c# - Entity Framework 代码优先 : Doesn't load referenced object

c# - 在 Multi-Tenancy .net 项目中实现角色的最佳方式是什么?

c# - 由于 MultiSelectList 无法将 IEnumerable<SelectListItem> 转换为字符串,Model.State 无效

c# - HTTP 错误 404.0 - 在 Mvc 6 Asp.net 5 中找不到

odata - Web API 2 OData EDM异常

c# - EF 中虚拟属性的 Odata 问题

java - 使用 OKHttp 访问 Graph Microsoft 上的用户个人资料返回特殊字符