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

Configuring default value with fluent API doesn't support enum value without explicitly casting to int #4286

Closed
EvAlex opened this issue Jan 12, 2016 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@EvAlex
Copy link

EvAlex commented Jan 12, 2016

Suppose we have model class with property of enum type that should be mapped to INT column in database. The class looks as follows

public class Employee
{
    public int Id { get; set; }

    public OccupationType OccupationType { get; set; }
}

public enum OccupationType 
{
    Fulltime,
    Parttime,
    Freelance
}

Also we want to configure default value for that column with fluent API as follows:

builder.Entity<Employee>()
    .Property(e => e.OccupationType)
    .HasDefaultValue(OccupationType.Fulltime);

Everything looks fine. Let's generate migration:

dnx ef migrations add AddEmployees

Here's what we get:

System.NotSupportedException: Annotations of type 'OccupationType' are not supported. Only common simple .NET types are currently supported.
   at Microsoft.Data.Entity.Infrastructure.TypedAnnotation.CheckType(Type type)
   at Microsoft.Data.Entity.Infrastructure.TypedAnnotation..ctor(Object value)
   at Microsoft.Data.Entity.Metadata.RelationalPropertyAnnotations.SetDefaultValue(Object value)
   at Microsoft.Data.Entity.Metadata.RelationalPropertyAnnotations.set_DefaultValue(Object value)
   at Microsoft.Data.Entity.RelationalPropertyBuilderExtensions.HasDefaultValue(PropertyBuilder propertyBuilder, Object value)
   at Microsoft.Data.Entity.RelationalPropertyBuilderExtensions.HasDefaultValue[TProperty](PropertyBuilder`1 propertyBuilder, Object value)
   at SomeProject.Models.ApplicationDbContext.OnModelCreating(ModelBuilder builder) in C:\Users\Alexander\Work\Github\EvAlex\SomeProject\src\SomeProject.Website\Models\ApplicationDbContext.cs:line 24
   at Microsoft.Data.Entity.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.Data.Entity.Infrastructure.ModelSource.<>c__DisplayClass8_0.<GetModel>b__0(Type k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Data.Entity.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.Data.Entity.Internal.DbContextServices.CreateModel()
   at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
   at Microsoft.Data.Entity.Internal.DbContextServices.get_Model()
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_5(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Data.Entity.Design.Internal.DesignTimeServicesBuilder.<>c__DisplayClass7_0.<ConfigureContextServices>b__8(IServiceProvider _)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass6_0.<AddMigration>b__0()
   at Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action)

It turns out that the problem is default value type. The error is in default value configuration and the correct syntax is:

builder.Entity<Employee>()
    .Property(e => e.OccupationType)
    .HasDefaultValue((int)OccupationType.Fulltime);
//                   ^^^^^ here's the difference

The exception message is absolutely not clear.

@rowanmiller
Copy link
Contributor

Related to #4061 (though that issue says we scaffold the migration, where as this issue fails generating the migration).

@rowanmiller rowanmiller added this to the 7.0.0-rc2 milestone Jan 12, 2016
@bricelam
Copy link
Contributor

@smitpatel Upon closer inspection, it looks the stack trace is purely in model building (getting DbContextServices.Model). Would you mind taking a look at this one?

@bricelam bricelam assigned smitpatel and unassigned bricelam Jan 12, 2016
@smitpatel
Copy link
Contributor

The issue lies in TypedAnnotation. It supports a fixed set of types only. So having any other default value type would throw similar exception.
TypedAnnotation is being used by DefaultValue API only. It was added when Annotation did not support object type value. Is it still needed?
It was added by @ajcvickers in #861

@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
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-bug
Projects
None yet
Development

No branches or pull requests

5 participants