Primary key from object instead of a native

I'm making a school asignement and we have to use a personally made class called EmployeeNumber as a primary key. I've tagged the field with [key] in the concerning class, but I keep getting the error that the EF can't find a primary key. The following class (employeenumber) https://hatebin.com/temmimpnee is a 'convertible' class, or should be, given the implicit operators. To my knowledge this should constitute to EF that the class should be saved in the SQL Table as a String but it doesn't seem to recognize it? Given the EmployeeConfiguration to equally define a key https://hatebin.com/gfhbwbzahf and the Employee class https://hatebin.com/ilhlrjtuee can anyone help me figure out why it doesnt take the primary key ? most of this code is given by the assignment, so most questions about 'why did you do it this way' is probably answered with :'because the code was given to us this way : s ' I apologize in advance.
6 Replies
WEIRD FLEX
WEIRD FLEX7mo ago
if you have already a Configure method then why is there a [Key] attribute on Id and also a [Column("Id")] ...and also a [Required]' while it's a nullable?!
Absolem Jackdaw
Absolem Jackdaw7mo ago
because the configure didnt do anything, I added the key annotation on top of it and then i added column id in the hopes that it would help/change. it used to be named number too, then i renamed it 'Id' and i added requiered hoping that it would be detected it shouldnt be nullable btw i believe a key shouldnt be nullable at all ?
WEIRD FLEX
WEIRD FLEX7mo ago
foreign key could be nullable primary can't i believe it has to be called somewhere in your code
Absolem Jackdaw
Absolem Jackdaw7mo ago
let me look up where i call it
WEIRD FLEX
WEIRD FLEX7mo ago
it should be in the DbContext
Absolem Jackdaw
Absolem Jackdaw7mo ago
ConfigurationManager configuration = builder.Configuration;
builder.Services.AddDbContext<HumanResourcesContext>(options =>
{
string connectionString = configuration["ConnectionString"];
options.UseSqlServer(connectionString, sqlOptions => { sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); });
#if DEBUG
options.UseLoggerFactory(LoggerFactory.Create(loggingBuilder => loggingBuilder.AddDebug()));
options.EnableSensitiveDataLogging();
#endif
});
ConfigurationManager configuration = builder.Configuration;
builder.Services.AddDbContext<HumanResourcesContext>(options =>
{
string connectionString = configuration["ConnectionString"];
options.UseSqlServer(connectionString, sqlOptions => { sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); });
#if DEBUG
options.UseLoggerFactory(LoggerFactory.Create(loggingBuilder => loggingBuilder.AddDebug()));
options.EnableSensitiveDataLogging();
#endif
});
this is in program.cs and i was told, or rather I currently believe(d) that the entityconfiguration was called here my dbcontext is as good as empty
internal class HumanResourcesContext : DbContext
{
public DbSet<Employee> Employees { get; set; }

public HumanResourcesContext(DbContextOptions options) : base(options)
{
}
}
internal class HumanResourcesContext : DbContext
{
public DbSet<Employee> Employees { get; set; }

public HumanResourcesContext(DbContextOptions options) : base(options)
{
}
}
let me put my onmodelcreating method in there ... here's the next error haha that worked seems like i forgot all about the onmodelcreating ... looked for 2 horus yeterday night before giving up CatThumbsUp for rubber ducking for me dont, i appreciate it