Page 1 of 1
EF Core 3.1 - Guid primary key not generated
Posted: Wed 19 Feb 2020 10:36
by kakone
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.
Re: EF Core 3.1 - Guid primary key not generated
Posted: Tue 25 Feb 2020 15:09
by Shalex
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>();
Re: EF Core 3.1 - Guid primary key not generated
Posted: Wed 26 Feb 2020 12:13
by kakone
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.
Re: EF Core 3.1 - Guid primary key not generated
Posted: Wed 04 Mar 2020 06:12
by slmnn
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.
Re: EF Core 3.1 - Guid primary key not generated
Posted: Fri 06 Mar 2020 16:52
by Shalex
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).
Re: EF Core 3.1 - Guid primary key not generated
Posted: Fri 06 Mar 2020 16:55
by Shalex
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();
Re: EF Core 3.1 - Guid primary key not generated
Posted: Mon 09 Mar 2020 09:53
by kakone
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.