Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I have an entity which has a string id.
The is is autogenerated by a function into the database.
I'm using the nugget package entityframeworkcore.sqlserver version 7.2 to store my entity into my database but somehow things don't work as expected.

The id is written into my database event tho I have set all parameters to let it know its autogenerated on add.

I did some research you can't make string Key with autogenerated properties but this should be supported later on so why is this not working for me.

When manually adding a record to the database the database generated function works fine.

Can someone tell me what to do.

[Table("Users")]
public class IdentityUser
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string Id { get; set; }
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;
}


My entityframework is this

public class UserStore : DbContext
    {
        private const string _connectionString = "" //this is a hidden working connectionstring;

        public DbSet<IdentityUser> Users { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(_connectionString);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityUser>().HasKey(x => x.Id);
            modelBuilder.Entity<IdentityUser>().Property(x => x.Id).ValueGeneratedOnAdd();
        }
    }


What I have tried:

google search
chatgpt help

couldn't find the solution to my problem.
Posted
Updated 26-Jan-23 1:54am
v2

1 solution

Here is a good Google Search: entity framework tutorials - Google Search[^]

This one, in the search results, I used a bit when needed: Entity Framework Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900