EntityFramework DatabaseGenerated

Asked

Viewed 81 times

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...

  • 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.

  • Possible duplicate of Error saving to bank

  • @gabrielcoletta not exactly. I saw this post.

  • 1

    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?

  • i edited the post.. I think it’s now clearer.

  • If this is a specific case, implement your separate entity and pass the [DatabaseGenerated(DatabaseGeneratedOption.None)]

  • 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

Show 3 more comments

1 answer

1


What you should do is enable the ability to insert explicit values in the identity column of your table. This is done by the parameter

SET IDENTITY_INSERT [TABELA] {ON|OFF}

Imagine we have the following model:

public class Blog {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int BlogId { get; set; }
    public string Titulo { get; set; }
}

The section of the method that makes the insertion in your bank should look like this:

if (blog.Titulo == tituloDeterminado) {
    using (var transaction = _context.Database.BeginTransaction()) {
        blog.BlogId = valorDeterminado;
        _context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Blog] ON");
        _context.SaveChanges();
        _context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Blog] OFF");
        transaction.Commit();
    }
}
else {
    await _context.SaveChangesAsync();
}

The code snippet will assign a certain valor (variable valorDeterminado) whenever the titulo is equal to a certain value (tituloDeterminado).

Take this as an example and not as a final solution for your project. Adapt it to your needs.

Source:

https://docs.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.