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 |1 Answer
Reset to default 0I was consistently getting empty guids (00000000-0000-0000-0000-000000000000
) instead of a NEWID()
until I added this:
...HasDefaultValueSql("NEWID()")
.ValueGeneratedOnAdd();
本文标签:
版权声明:本文标题:c# - Using HasDefaultValueSql to generate a GUID when it's optional in DTO but required in database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300754a1930933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:58NEWID()
– Francis Ducharme Commented Nov 22, 2024 at 22:31SaveChanges
. After that, the ID generated by the database will be accessible automatically. When you configure the entity withHasDefaultValueSql()
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