问题
I'm using EF 5.0 Code First, in a .NET 4 library. Trying to set a class member to be of an Enum
type, but it doesn't get persisted to the database, and no runtime exception occurs when EF is creating the database based on the code.
Of course, when I try to query against that Enum:
var departments = db.Departments
.Where(dep => dep.Name == DepartmentNames.English);
I get an exception saying:
The specified type member 'Name' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
I am using Microsoft's example of an Enum use: http://msdn.microsoft.com/en-us/data/hh859576.aspx
Are Enum members not supported yet? Does EF 5.0 differs somehow when hosted by a .NET 4 environemt than when hosted by a .NET 5 one?
回答1:
Enums where first introduced in EF 5 components that live in .NET Framework 4.5 (System.Data.Entity.dll). If you are using EF5 but target .NET Framework 4 there is no support for enums since they are not supported by the System.Data.Entity.dll that is part of .NET Framework 4. So, yes EF5 on .NET Framework 4 is scoped down when comparing to EF5 on .NET Framework 4.5. Some other features that are available when you target .NET Framework 4.5 but not available when you target .NET Framework 4 are spatial types, Table Valued Functions, strored procedures returning multiple resultsets and a couple more. An alternative is to move to EF6 (Alpha release shipped a few weeks ago) which does not depend on System.Data.Entity.dll and therefore supports all features that shipped in EF5 on both .NET Framework 4.5 and .NET Framework 4 plus some more (e.g. async on .NET Framework 4.5). EF6 is an open source project from Microsoft and you can find all details here: http://entityframework.codeplex.com/
来源:https://stackoverflow.com/questions/13407957/enum-type-not-being-mapped-to-db-table