简体   繁体   English

扩展实体框架属性

[英]Extending an Entity Framework property

I have an entity called File, I created a partial class with a property on it. 我有一个称为File的实体,我创建了一个带有属性的部分类。

I was able to use this property in regular constructors and other statements. 我能够在常规构造函数和其他语句中使用此属性。 But this property is not accepted in any linq statement. 但是,任何linq语句均不接受此属性。

I am getting the following exception. 我收到以下异常。

Exception 例外

The specified type member 'FileStatus' is not supported in LINQ to Entities. LINQ to Entities不支持指定的类型成员'FileStatus'。 Only initializers, entity members, and entity navigation properties are supported. 仅支持初始化程序,实体成员和实体导航属性。

Code

var fileEntry = from entry in context.ICFiles
                             where entry.FileName == FileName &&
                                   entry.FileStatus == FileStatus.InProgress
                             select entry;

fileEntry.First().FileStatus = FileStatus.Completed; 
// This is where I get the exception

Property Definition 属性定义

public partial class ICFile
{
    [DataMemberAttribute()]
    public FileStatus FileStatus
    {
        get
        {
            return (FileStatus)this.Status;
        }
        set
        {
            this.Status = (int)value;
        }
    }
}

I think the problem is that the entity framework is trying to convert your FileStatus property to a column in the database, where it does not exist and there is no mapping information for it to go on as you have defined it in a partial class. 我认为问题在于实体框架正在尝试将FileStatus属性转换为数据库中的一列,该列不存在,并且在部分类中对其进行定义时也没有映射信息可以继续进行。

I see that you are trying to cast to an enumeration, because presumably you are using an Entity Framework version before the version 5 beta that apparently has enum support (I may be wrong on that, perhaps it is still missing). 我看到您正在尝试进行枚举,因为大概您是在使用显然具有枚举支持的版本5 beta之前使用的Entity Framework版本(对此我可能是错的,也许仍然缺少)。

Sadly enumerations do not really work very well in the current Entity Framework version (4.3.1). 遗憾的是,在当前的Entity Framework版本(4.3.1)中,枚举实际上并不能很好地工作。 There are various kludges you can do but none are very satisfactory and you often end up with accidentally loading the entire table into memory or something equally horrendous (see Using ADO.net Entity Framework 4 with Enumerations? How do I do it? ). 您可以进行各种各样的操作,但都不是很令人满意,并且您经常最终会意外地将整个表加载到内存中或其他同样可怕的事情(请参阅将ADO.net Entity Framework 4与Enumerations一起使用?我该怎么做? )。

Personally I would just not bother with the partial class and do your query like this, to keep things simple: 就个人而言,我只是不会打扰部分类,而是像这样进行查询,以使事情变得简单:

var fileEntry = from entry in context.ICFiles
                         where entry.FileName == FileName &&
                               entry.Status == (int)FileStatus.InProgress
                         select entry;

fileEntry.First().Status = (int)FileStatus.Completed; 

Not ideal, but it works and it shows what is happening and you know that the where clause will be correctly translated to an equivalent bit of SQL to run against the database (I think that is called "pragmatic"). 这不是理想的方法,但是它可以工作,并且可以显示正在发生的事情,并且您知道where子句将正确转换为等效的SQL语句,以针对数据库运行(我认为这称为“实用”)。

I don't think the way you use Enums in your code example is causing any issues. 我认为在代码示例中使用枚举的方式不会引起任何问题。 The problem is simply that your class extension property is not recognized by EF as a member of ICFiles since it is not really in your edmx model nor a column in your database table. 问题很简单,因为EF不能将您的类扩展属性识别为ICFiles的成员,因为它实际上不在edmx模型中,也不在数据库表中的列中。 Use the code example user1039947 gave you and you should be fine. 使用user1039947给您的代码示例,您应该会很好。

You can however use your original query when doing a LINQ to Object search on an ICFiles IEnumerable instance. 但是,当对ICFiles IEnumerable实例执行LINQ to Object搜索时,可以使用原始查询。 This is because LINQ to Entities is translated into SQL before fetching data from the database, while LINQ to objects is done in memory. 这是因为LINQ to Entities在从数据库中获取数据之前已转换为SQL,而LINQ to Object是在内存中完成的。 Your FileStatus property does not exist in your database, only in memory. 您的FileStatus属性在数据库中不存在,仅在内存中。

Right when I asked the question, I was left with the answer that @user1039947 has posted. 就在我问这个问题时,我留下了@ user1039947发布的答案。 However, that's not the one I was expecting as an answer for this question. 但是,这不是我期望作为该问题的答案的那个。

I was looking for the ability of Entity framework to utilize the Enum completely. 我一直在寻找Entity框架完全利用Enum的能力。 But eventually I figured out that Linq to Entity can't recognize the Enums (it usually recognizes the entities through an attribute starting with Edm, like EdmTypeAttribute, EntityPropertyAttribute etc.) 但是最终我发现Linq to Entity无法识别枚举(它通常通过以Edm开头的属性(例如EdmTypeAttribute,EntityPropertyAttribute等)识别实体。

Linq to Entity of Framework 4.0, has no way to accept the enums as entity object. Linq to Framework 4.0的实体,无法将枚举作为实体对象接受。 However, the partial method serves us 80% what we might need from enum. 但是,部分方法为我们提供了枚举可能需要的80%的服务。

I have posted a blog with more detail on this. 我已经发布了一个博客,其中有更多详细信息。

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

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