Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Model-First Enum Type MetadataType

I know when working with model-first development, you can use partial classes generated by the t4 templates to add metadata. e.g.

public partial class Address
{        
    public int Id { get; set; }
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

Then in a separate file, I do:

[MetadataType(typeof(AddressMetadata))]
public partial class Address {
}

internal sealed class AddressMetadata {

    [Display(Name = "Street")]
    public string Street1 { get; set; }

    [Display(Name = "Street (cont.)")]
    public string Street2 { get; set; }

    [Display(Name = "Zip code")]
    public string Zip { get; set; }
}

I'm trying to do this for an enum type defined within the EDMX file.

// this doesn't work
[MetadataType(typeof(ContactTypeMetadata))]
public enum ContactType {

}

public class ContactTypeMetadata {
}

Doing this, I get the following error:

Error   1   The namespace 'Models' already contains a definition for 'ContactType'

Is there anyway to do the same functionality for the enumerations as you can do for the classes in a model-first project?

EDIT

Within the EDMX file, I've defined an enum type:

namespace WindowsFormsApplication1
{
    using System;

    public enum ContactType : int
    {
        CEO = 0,
        CIO = 1,
        Peasant = 2
    }
}

I'm trying to find a way using a similar mechanism (in separate files so that if I modify the EDMX my changes won't get overwritten) to accomplish this:

namespace WindowsFormsApplication1
{
    using System;

    public enum ContactType : int
    {
        [Display(Name="Chief Executive Officer")]
        CEO = 0,

        [Display(Name="Chief Information Officer")]
        CIO = 1,

        [Display(Name="Regular Employee")]
        Peasant = 2
    }
}
like image 800
Dan Champagne Avatar asked Dec 19 '22 12:12

Dan Champagne


1 Answers

You need to disable code generation for your enum. You can do this by referencing an external type.

Create an enum outside of your .edmx. E.g. ContactType.cs in the root of your project, although you can place it wherever you like so long as you know what namespace it is in.

Copy out your current code from the edmx enum you already created and paste it into your newly created ContactType.cs file, as shown below.

ContactType

using System;
namespace YourEnumNamespace
{
    public enum ContactType : int
    {
        [Display(Name="Chief Executive Officer")]
        CEO = 0,

        [Display(Name="Chief Information Officer")]
        CIO = 1,

        [Display(Name="Regular Employee")]
        Peasant = 2
    }
}

Now in your .edmx Model delete your existing ContactType enum, this is located in the Model Browser under the Enum Types section, simply select it and delete it.

Model Browser

Go back to your Solution Browse, open your edmx and right click somewhere and click 'Add New' and then 'Enum Type'.

Add New Enum Type

Finally name your enum type ContactType and tick Reference External type, here fully qualify your custom enum you created as I showed earlier. E.g. YourEnumNamespace.ContactType.

This tells the edmx to use an external enum and not generate the enum code within the edmx itself.

New Enum

Finally within the entity within your edmx file that you wish to use your custom enum with, select the property that you want to use with your custom enum, in this case I've called it MyContactTypeProperty

Entity with Property

And change the property type to your custom enum.

enum property

And there you go. No need to frig around trying to declare partial enums and the like, for the record you can't have a partial enum. Anyway this will do what you need. Good luck!

like image 62
Daniel Lane Avatar answered Jan 06 '23 04:01

Daniel Lane