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

Mapping identity columns to enum properties not working in 3.1.5 / 3.1.8 / 5.0.0-preview.8.20407.4 #22446

Closed
cceuroform opened this issue Sep 8, 2020 · 1 comment

Comments

@cceuroform
Copy link

cceuroform commented Sep 8, 2020

I use proper enumerations to prevent mixing int from different tables.
Based on:
#11970
#13814
I wrote the provided code.

When running the code I got the following error:

Identity value generation cannot be used for the property 'DeviceID' on entity type 'Device' because the property type is 'Device_Values'. Identity value generation can only be used with signed integer properties.

I have tried the code with the following versions of EF Core

  • 3.1.5
  • 3.1.8
  • 5.0.0-preview.8.20407.4

Can you provide any guidance/workaround for how to fix this?

Steps to reproduce

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;
using System;

namespace EFCoreTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using var ctx = new MyContext();
            ctx.Devices.Add(new Device());
            ctx.SaveChanges();
        }
    }
    public enum Device_Values { }
    public partial class Device
    {
        public Device_Values DeviceID { get; set; }
    }
    public class MyContext : DbContext
    {
        public virtual DbSet<Device> Devices { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var converter = new ValueConverter<Device_Values, int>(v => (int)v, v => (Device_Values)v, new ConverterMappingHints(valueGeneratorFactory: (p, t) => new TemporaryIntValueGenerator()));
            modelBuilder.Entity<Device>(entity => entity.Property(e => e.DeviceID).ValueGeneratedOnAdd().UseIdentityColumn().HasConversion(converter));
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase("test");
            base.OnConfiguring(optionsBuilder);
        }
    }
}

Further technical details

EF Core version: 3.1.5 & 3.1.8 & 5.0.0-preview.8.20407.4
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.1)
Operating system: Windows 10
IDE: Visual Studio 2019 16.8 Preview

@ajcvickers
Copy link
Member

@cceuroform This is currently not supported--issue #11970, which you reference above, is tracking adding support for this.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants