admin管理员组

文章数量:1122826

I have a property declared as such on a DTO (the object received from the front end) and I'm trying to use HasDefaultValueSql but always ending up with empty guids, instead of legit guids.

Here's the set up

class CarDto 
{   
    public Guid? Id { get; init; }
}

I declare it as nullable, because when the client creates the entity, Id will either be missing, null or empty string so the backend generates the id itself.

If I don't declare it as nullable, the serialization process fails

The JSON value could not be converted to System.Guid. Path: $.id

I then have the entity configured as an EF entity as such

class Car 
{
    public Guid Id { get; set; }
}

Then in the OnModelCreating of my context, I configure the property:

modelBuilder.Entity<Car>()
            .Property(e => e.Id)
            .HasDefaultValueSql("NEWID()");

The problem is, when I convert from DTO to database object, Id becomes an empty GUID (00000000-0000-0000-0000-000000000000) because it's not nullable at this point.

var carToCreate = mapper.Map<Car>(carDto);
//carToCreate.Id becomes empty guid, not a null.

So I think that is why HasDefaultValueSql has no effect since there is a value, so no need to put a default one.

How can this even work? Seems simple enough but it eludes me.

I have a property declared as such on a DTO (the object received from the front end) and I'm trying to use HasDefaultValueSql but always ending up with empty guids, instead of legit guids.

Here's the set up

class CarDto 
{   
    public Guid? Id { get; init; }
}

I declare it as nullable, because when the client creates the entity, Id will either be missing, null or empty string so the backend generates the id itself.

If I don't declare it as nullable, the serialization process fails

The JSON value could not be converted to System.Guid. Path: $.id

I then have the entity configured as an EF entity as such

class Car 
{
    public Guid Id { get; set; }
}

Then in the OnModelCreating of my context, I configure the property:

modelBuilder.Entity<Car>()
            .Property(e => e.Id)
            .HasDefaultValueSql("NEWID()");

The problem is, when I convert from DTO to database object, Id becomes an empty GUID (00000000-0000-0000-0000-000000000000) because it's not nullable at this point.

var carToCreate = mapper.Map<Car>(carDto);
//carToCreate.Id becomes empty guid, not a null.

So I think that is why HasDefaultValueSql has no effect since there is a value, so no need to put a default one.

How can this even work? Seems simple enough but it eludes me.

Share Improve this question edited Dec 4, 2024 at 1:54 Zhi Lv 21.2k1 gold badge26 silver badges36 bronze badges asked Nov 22, 2024 at 21:26 Francis DucharmeFrancis Ducharme 4,9877 gold badges46 silver badges85 bronze badges 4
  • What is "an empty GUID", you mean Guid.Empty? If so, that's not a problem. EF will consider the Id value as not set and replace it by the generated value. – Gert Arnold Commented Nov 22, 2024 at 21:58
  • @GertArnold Indeed, I edited the question. When the property is set to Guid.Empty before saving changes, seems like EF/SQL considers this a value that doesn't need to be overwritten by executing the NEWID() – Francis Ducharme Commented Nov 22, 2024 at 22:31
  • The Car entity will initially have a Guid.Empty value until you call SaveChanges. After that, the ID generated by the database will be accessible automatically. When you configure the entity with HasDefaultValueSql() EF sets up to recognize that the database will be responsible for inserting a new ID. You cannot get that ID back until the record is saved. If you want it generated client side in the entity then you can initialize a new GUID in the entity and mark with [DatabaseGenerated(DatabaseGeneratedOption.None)] Note: GUIDs make for poor PKs due to index fragmentation. – Steve Py Commented Nov 22, 2024 at 23:08
  • Re Steve py's last note consider NewSequentialId() vs NewId(), but if you're never going to have more than 4 billion cars, use int (and 18 quintillion cars use bigint); why needlessly give the DB more bytes to compare in every single join it ever does? – flackoverstow Commented Nov 23, 2024 at 8:52
Add a comment  | 

1 Answer 1

Reset to default 0

I was consistently getting empty guids (00000000-0000-0000-0000-000000000000) instead of a NEWID() until I added this:

...HasDefaultValueSql("NEWID()")
        .ValueGeneratedOnAdd();

本文标签: