EF Core 3.1 - Guid primary key not generated

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
kakone
Posts: 12
Joined: Thu 09 Jan 2020 17:30

EF Core 3.1 - Guid primary key not generated

Post by kakone » Wed 19 Feb 2020 10:36

Hello,

With EF Core 3.1, my primary key is a Guid property :

Code: Select all

public class UserEquipment
{
    public Guid Id { get; set; }
    public int UserId { get; set; }
    public int EquipmentId { get; set; }
}

Code: Select all

var userEquipment = modelBuilder.Entity<UserEquipment>();
userEquipment.ToTable("USER_EQUIPMENT").HasKey(e => e.Id);
userEquipment.Property(e => e.Id).HasColumnName("ID");
When I insert a new record, I got the ORA-01400 exception: : cannot insert NULL into ("SCHEMA"."USER_EQUIPMENT"."ID") because the Guid value is not generated. I tried to add the ValueGeneratedOnAdd() method but the result is the same.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: EF Core 3.1 - Guid primary key not generated

Post by Shalex » Tue 25 Feb 2020 15:09

You are using the Code-First approach, aren't you?

Possible solutions:

1) generate Id in the class constructor

Code: Select all

public UserEquipment() {
    Id = Guid.NewGuid();
}
2) use value generator

Code: Select all

.ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();

kakone
Posts: 12
Joined: Thu 09 Jan 2020 17:30

Re: EF Core 3.1 - Guid primary key not generated

Post by kakone » Wed 26 Feb 2020 12:13

Yes, I'm using the code-first approach. The solution 1 is a possible workaround but the problem is precisely that the GuidValueGenerator does not work.
Logically, without specifying anything, it should already work. But when I add

Code: Select all

.ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();
, it doesn't work either.

slmnn
Posts: 2
Joined: Tue 03 Mar 2020 06:44

Re: EF Core 3.1 - Guid primary key not generated

Post by slmnn » Wed 04 Mar 2020 06:12

We are experiencing the same issue with the latest dotConnect for Oracle (9.11). Currently we are holding back from updating version 9.9.887 as it seems to work. Our Fluent API configuration is:

Code: Select all

        
            builder.ToTable("TABLE_NAME")
                .HasKey(e => e.Id);

            builder.HasIndex(e => new { e.Id })
                .IsUnique();

            builder.Property(e => e.Id)
                .ValueGeneratedOnAdd();
                
            ...
 
Where Id property is a GUID.

So in our case, something is broken in dotConnect for Oracle after 9.9.887.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: EF Core 3.1 - Guid primary key not generated

Post by Shalex » Fri 06 Mar 2020 16:52

kakone wrote: Wed 26 Feb 2020 12:13 Yes, I'm using the code-first approach. The solution 1 is a possible workaround but the problem is precisely that the GuidValueGenerator does not work.
Logically, without specifying anything, it should already work. But when I add

Code: Select all

.ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();
, it doesn't work either.
It works in our environment:

Code: Select all

  modelBuilder.Entity<FORUM39872>().Property<System.Guid>(x => x.ID).HasColumnName(@"ID").IsRequired().ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();
  
...

    var entity = new FORUM39872() { NAME = "a" };
    context.FORUM39872S.Add(entity);
    context.SaveChanges();
    
->

INSERT INTO FORUM39872S (ID, NAME)
VALUES (:p0, :p1)

:p0 = {90b4894f-1099-41f2-b0be-6dc4f01f8b4b}
:p1 = "a"
Please create a small test project for reproducing and upload it to some file exchange server (e.g.: ww.dropbox.com).

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: EF Core 3.1 - Guid primary key not generated

Post by Shalex » Fri 06 Mar 2020 16:55

slmnn wrote: Wed 04 Mar 2020 06:12 We are experiencing the same issue with the latest dotConnect for Oracle (9.11). Currently we are holding back from updating version 9.9.887 as it seems to work. Our Fluent API configuration is:

Code: Select all

        
            builder.ToTable("TABLE_NAME")
                .HasKey(e => e.Id);

            builder.HasIndex(e => new { e.Id })
                .IsUnique();

            builder.Property(e => e.Id)
                .ValueGeneratedOnAdd();
                
            ...
 
Where Id property is a GUID.

So in our case, something is broken in dotConnect for Oracle after 9.9.887.
Please use

Code: Select all

builder.Property(e => e.Id).ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();
instead of

Code: Select all

builder.Property(e => e.Id).ValueGeneratedOnAdd();

kakone
Posts: 12
Joined: Thu 09 Jan 2020 17:30

Re: EF Core 3.1 - Guid primary key not generated

Post by kakone » Mon 09 Mar 2020 09:53

After rechecking, it works well with the version 9.11.951 if we specify .ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>(); on all of our columns of Guid type.

Thanks.

Post Reply