0
I have a property in my model where automatically generates the Id when it is inserted in Db, but I have a specific case where I need to put the Id manually, there is some way to prevent the EF from generating the Automatic Id, if I create and send the Object with the fixed id it ignores and generates a new one.
My question is: is it possible when saving to have an option to ignore automatic generation? In a specific case, the generation should be automatic as the code below. Just to clarify, the problem is in the Seed of the database, where I need to add a user with a fixed Id where it is related to the properties and other relationships of another system, in the others will be generated automatically therefore: I cannot remove this automatic generation property from ID.
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override Guid Id
{
get => base.Id;
set => base.Id = value;
}
You have to check if the base is being automatically generated and remove it from there and in your code remove
DatabaseGenerated
...– novic
It is being generated automatically yes.. but I cannot remove it from my code.. i need to know if there is any optional parameter to ignore automatic generation, because it is a specific case where I need to define the Id, in the other generation is auto.
– Thiago Loureiro
Possible duplicate of Error saving to bank
– Gabriel Coletta
@gabrielcoletta not exactly. I saw this post.
– Thiago Loureiro
Thiago, this configuration has to be removed from your table and also from your code is what has to be done .... Even putting None, the table will generate a new identifier, although I believe that your doubt is very strange, since it is a Guid, because in case you want to control, it is not better the bank and the Orm?
– novic
i edited the post.. I think it’s now clearer.
– Thiago Loureiro
If this is a specific case, implement your separate entity and pass the
[DatabaseGenerated(DatabaseGeneratedOption.None)]
– Gabriel Coletta
Thanks Gabriel... really that’s the detail.. It’s a specific case, if it were standard would do what Virgilio said, to remove, but it’s not.. so I quoted that at the beginning of the problem in question.. Thank you all for the help. abs
– Thiago Loureiro