我想弄清楚如何将 NotMapped 属性与 OData 一起使用
我有这个 用户模型 :
[DataContract]
public class User
{
[DataMember]
public long Id { get; set; }
[DataMember]
public virtual InstagramUser InstagramUser { get; set; }
[DataMember]
public virtual FacebookUser FacebookUser { get; set; }
[NotMapped]
public string InstagramID { get; set;
}
我在用户 Controller 中有这个 GetAll() 方法
public IQueryable<User> GetAllUsers()
{
_repository = new UserRepository();
return _repository.GetAll().AsQueryable();
}
以及用户存储库中的这个 GetAll() 函数
public IQueryable<User> GetAll()
{
InstaStoryDB db = new InstaStoryDB();
var ret = db.Users.Include("InstagramUser").Include("FacebookUser").AsQueryable();
foreach(User user in ret)
{
user.InstagramID = user.InstagramUser.InstagramID; //<-- I'm adding here the value to the property
}
return ret;
}
这是 InstagramUser 模型
[DataContract]
public class InstagramUser
{
[DataMember]
public long UserId { get; set; }
[DataMember]
public string InstagramID { get; set;
}
所有工作良好。
我对 User 模型进行了更改并添加了包含
InstagramUser
的 InstagramID
,因此我添加了 [NotMapped] 属性以从 数据库 的 User 表中删除 InstagramID,因为 InstagramUser 已经包含了 InstagramID。当我尝试像这样使用 Odata 时:
本地主机:555/api/users/?$filter=InstagramID eq '456456546'
它失败并返回 InstagramID 属性不存在的错误。
我该怎么做才能在 NotMapped 属性上使用带有 Odata 的过滤器?
最佳答案
这对我在 Web API 中使用 OData v4 有用:
var t = modelBuilder.StructuralTypes.First(x => x.ClrType == typeof(User));
t.AddProperty(typeof(User).GetProperty("InstagramID"));
关于asp.net-mvc - 将 NotMapped 属性值与 Odata $filter 函数一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14374284/