如何添加复杂属性EF Code First

时间:2013-04-25 14:39:42

标签: .net linq entity-framework code-first

我有三个简单的课程

public class Asset
{
    public int ID {get;set;}
    public virtual Category {get;set;}
}

public class Category 
{
    public int ID {get;set;}
}

public class AssetCollection
{
    public int ID {get;set;}
    public virtual ICollection<Asset> Assets {get;set;}

    ??????public bool HasAssetWithCategory(int ID)
    {
    ????????????
    }
}

我希望能够像这样选择上下文HasAssetWithCategory

var assets = context.AssetCollections.Where(m => m.HasAssetWithCategory(SomeID));

即使从哪​​里开始,我也没有线索。

1 个答案:

答案 0 :(得分:0)

HasAssetCategory是您已定义且无法转换为SQL语句的方法。因此,EntityFramework无法执行您拥有的查询。相反,您应该尝试此查询,

var assets = context.AssetCollections.Where(m => m.Assets.Any(a => a.ID == someID));