Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying a default value for a column corresponding to an enum model property #4081

Closed
eriksendc opened this issue Dec 14, 2015 · 4 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@eriksendc
Copy link

Hi All,

I have an enum as follows:

public enum FirstTimeSignInDestination { Unknown = 0, Homepage = 1, GuardianDashboard = 2, Search = 3, AdopterDashboard = 4 }

In the OnModelCreating() override of my DbContext-derived class I put the following:

builder.Entity<ApplicationUser>().Property(p => p.FirstTimeSignInDestination). HasDefaultValue(FirstTimeSignInDestination.Unknown);

This compiles, but when I do dnx ef migrations add FirstTimeSignInDestination I get the following error message:

Annotations of type 'FirstTimeSignInDestination' are not supported. Only common simple .NET types are currently supported.

Since I know FirstTimeSignInDestination.Unknown corresponds to 0 I just used 0 instead and everything works. It would be cool for this to work with one of the enum values as I'd tried, though.

Thanks for your consideration.

Regards,
-Brian Eriksen

@rowanmiller rowanmiller added this to the Backlog milestone Dec 16, 2015
@rowanmiller rowanmiller changed the title Issue specifying a default value for a column corresponding to an enum model property Support specifying a default value for a column corresponding to an enum model property Dec 16, 2015
@rykr
Copy link

rykr commented Sep 22, 2016

@rowanmiller

I have a similar issue. if I have an enum like this:

public enum MyEnum:int { ValA = 1, ValB = 2, ValC = 4 }

And then I specify an entity property like this:

public class BlahBlah {
.....
[DefaultValue(MyEnum.ValA)]
public MyEnum EnumVal { get; set; }
...
}

When that is generted, the "Up" migration method will add the EnumVal column and give it a default value of MyEnum.0. This doesn't compile and is not even a valid scalar value for the enum.

Is enum default values not supported? If this can be handled, how should I handle it?

@rowanmiller
Copy link
Contributor

Default enum values are not really supported yet (this issue is tracking that). The best way to handle it at the moment is to specify the default as the underlying type (i.e. 1 rather than MyEnum.ValA).

@rykr
Copy link

rykr commented Sep 22, 2016

Thanks. We'll do that and hopefully this will be supported in the future.

@bricelam
Copy link
Contributor

@smitpatel Do you know if this works now?

@ajcvickers ajcvickers modified the milestones: Backlog, 2.1.0 Feb 23, 2018
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed propose-close labels Feb 23, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

6 participants