C#EntityFramework 6.0 - 如何使用EntityTypeConfiguration中的Where语句? [英] C# EntityFramework 6.0 - How to use Where Statement in EntityTypeConfiguration?

查看:251
本文介绍了C#EntityFramework 6.0 - 如何使用EntityTypeConfiguration中的Where语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个类:

Parent.cs

Parent.cs

public class Parent
{
   public int Id {get;set;}
   public virtual ICollection<Child> Children { get; set; }
}

Child.cs

public class Child
{
    public int Id {get;set;}
    public ItemStatusType ItemStatusTyp { get; set; }
    public int ParentId {get;set;}

    [ForeignKey("ParentId")]
    public virtual Parent Parent { get; set; }
}

ItemStatusType.cs

ItemStatusType.cs

public enum ItemStatusType
    {
        Active = 1,
        Deactive = 2,
        Deleted = 3
    }

我想要的是以某种方式检索始终 活动而不是删除。由于我并没有在物理上删除记录,我只是将 ItemStatusType 更新为已删除状态。

What I want is to somehow retrieve always the active ones and not the deleted ones. Since I am not deleting the record physically, I'm merely updating the ItemStatusType to Deleted status.

所以,当我说 ParentObj.Children 我只希望检索活动的,而不再进一步使用 Where

So, when I say ParentObj.Children I only wish to retrieve the active ones without further using Where condition.

这是迄今为止所做的,但在之后提出的运行时发生异常:

Here is so far what I've done but giving an exception on runtime that I stated afterwards:

public class ParentConfiguration : EntityTypeConfiguration<Parent>
{
     public ParentConfiguration()
     {
             HasMany(c => c.Children.Where(p => p.ItemStatusTyp != ItemStatusType.Deleted).ToList())
                .WithRequired(c => c.Parent)
                .HasForeignKey(c => c.ParentId)
                ;
     }
}

运行时异常:


表达式'c => c.Children.Where(p =>(Convert(p.ItemStatusTyp)
!= 3))ToList()不是有效的属性表达式。表达式
应该表示一个属性:C#:'t => t.MyProperty'VB.Net:
'函数(t)t.MyProperty'。

The expression 'c => c.Children.Where(p => (Convert(p.ItemStatusTyp) != 3)).ToList()' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.

我不得不在表达式之后使用 ToList ,否则不会编译。

I had to use ToList after the expression, otherwise it does not compile.

什么是正确的我要做什么?

What is the proper what to do what I want?

提前感谢,

推荐答案

您不能使用其中或任何其他逻辑在流畅的属性映射 - 这只是配置。

You cannot use Where or any other logic in fluent property mapping - that's just configuration.

基本上你不能以声明的方式解决你需要的东西。

Basically you cannot solve what you need in declarative way.

有一些解决方法可以用于一级实体,比如实现你自己的扩展(s) MySet< T> 将返回 .Set< T> .Where(x => x.ItemStatusType!= ItemStatusType.Deleted) 并使用它到处,但这不会解决儿童集合的过滤问题。

There are some workarounds which you can use for first-level entities, like implement your own extension(s) MySet<T> which will return .Set<T>.Where(x => x.ItemStatusType != ItemStatusType.Deleted) and use it everywhere, but that won't solve filtering issue for child collections.

你可以努力的方式准备一个单独的用于选择数据的实体,其基本上应该基于数据库视图或存储过程;您将不得不为每个实体创建单独的视图,因此您可以根据这些视图实体组合选择任何关系。

You can go hard way and prepare a separate set of entities to use for selecting data, which basically should be based on database views or stored procedures; you will have to create separate view for every entity, so you will be able to combine selecting in any relations based on these views-entities.

为了插入,虽然您将需要将实体映射到真实表。不确定它是否值得,但在某些情况下可能。

For inserting though you will need to have entities mapped over "real" tables. No sure if it worth it but it might in some cases.

这篇关于C#EntityFramework 6.0 - 如何使用EntityTypeConfiguration中的Where语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆