1

When it comes to ASP Identity, I created a new table, custom table using code first and migration (code below). When I try to inset into that table using DbContext I have the id cannot be null error from entity framework. I tried using id with annotation as computed / identity and the error is the same.

For sure, I missed something but I cannot figure out what.

public class AspNetCountryTable
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public String Id { get; set; }
        public String CountryName { get; set; }
        public String CountryCode { get; set; }
    }

What I want to do is to have and automated generated Id (String) just the way asp identity is generating the id for the user.

Thanks!

6
  • Did you update your data base so that it actually generates an ID? Commented Jun 20, 2017 at 8:26
  • @Fabiano I used add-migration "new_migration_name" and update-database from package console to generate (in theory) the new sql table. Was there something else to do? Commented Jun 20, 2017 at 8:28
  • Could you try with [DatabaseGenerated(DatabaseGeneratedOption.None)] instead of [DatabaseGenerated(DatabaseGeneratedOption.Identity)]? Commented Jun 20, 2017 at 8:33
  • String is a nullable type, hence it doesn't use computed values unlike int does when applying DatabaseGeneratedOption.Identity option. Try using [DatabaseGenerated(DatabaseGeneratedOption.None)] instead, or follow similar steps here and here but instead of GUID it stores string. Commented Jun 20, 2017 at 8:33
  • 5
    What do expect SQL to generate an identity column from string? Commented Jun 20, 2017 at 8:39

1 Answer 1

0

Had to add at SQL level Id column as uniqueidentifier instead of string and default sql value in migration script: newsequentialid()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.