Im new to Entity framework 5 and Im trying the enum support but Im having some problems with it. Im using EF5 as I used the previous versions, this is to create the database and generate the model from the database and use code generation. So, I change the code generation property of the model to “default” and I remove the .tt files.
I have a solution with multiple dlls, one of them has the DataModel (edmx) and another one has an enum called “EnumGender” which has 2 values: male and female.
In the db I have a table Users which has a field called “Gender” (smallint) and im trying to use the “EnumGender” for it.
EnumGender is in a namespace called “Sample.Datatypes.Enums”, so in the edmx model browser im trying to add a Enum. For this I set “EnumGender” as the name, I didnt add any value and I checked the checkbox “Reference external type” and I entered “Sample.Datatypes.Enums.EnumGender” in the textbox.
The problem is that it seems that EF is creating a new enum in the data model namespace, which I dont want, I would like to use EnumGender in the “Sample.Datatypes.Enums” namespace. Also the new enum that the EF creates has no values… If I enter to the datamodel cs file (the code generated) this is what it added:
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEnumTypeAttribute(NamespaceName="SampleModel", Name="EnumGender")]
[DataContractAttribute()]
public enum EnumGender : short
{
}
I have no clue of what is happening… maybe I cant use a datatype from an external dll… But for what I read about the enums support I should be able to do this…
I would really appreciate it if you can help me with this issue.
Thanks!
Juan
Though not obvious, but this is by design. The EntityObject template (the one used when you set code generation property of the model to “default”) doesn’t use external types because it requires the types to have certain attributes on them (EdmEnumTypeAttribute and DataContractAttribute for enums). This constraint can only be reliably satisfied if the type itself is also generated by the template.
I would highly recommend using the DbContext template (the .tt files that were added by default) as it uses POCO types and thus supports external types.
However if your scenario absolutely requires you to use the EntityObject template there’s a way to change it to use the external enum type:
If you are working on a web project then use this one instead: http://visualstudiogallery.msdn.microsoft.com/94b48556-fcf0-4b9b-8615-20f9066ae9ac
public string SourceCsdlPath{ get; set; }and add this line before it:7. Search for
GetSourceSchemaTypes<EnumType>().OrderBy(c => c.Name)and replace it with:8. Search for
string typeName = MultiSchemaEscape(usage.EdmType, code);and replace it with: