Key property excluded from SQL
See original GitHub issueWhen you have an entity with a Guid
key, that is not set to get its value from the DB, Upsert still excludes it because EF sets the ValueGenerated
property to OnAdd
, so the following line:
if (prop.ValueGenerated != ValueGenerated.Never) continue;
excludes the key and I get a SQL error saying the key column doesn’t accept nulls. The key is configured as such:
class Entity
{
public Guid Id { get; set; }
public string Name { get; set; }
}
class MyContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Entity>()
.HasKey(x => x.Id);
}
}
No idea why EF specifies it this way, but the model works when inserting with EF normally (i.e. uses the value on the entity, rather than generating it on the DB)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:15 (8 by maintainers)
Top Results From Across the Web
Syntax and Examples of SQL EXCLUDE
EXCLUDE conditions in SQL usually appear in the WHERE clause of the statement or in the HAVING clause of an aggregate query. Some...
Read more >c# - Exclude primary key in model in .NET Core as SQL ...
I tried now following code inside my model for ID property: [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; ...
Read more >Primary and Foreign Key Constraints - SQL Server
Primary keys and foreign keys are two types of constraints that can be used to enforce data integrity in SQL Server tables.
Read more >SQL Query to Exclude Records if it Matches an Entry in ...
In this article, we will see, how to write the SQL Query to exclude records if it matches an entry in another table....
Read more >sp_dropextendedproperty (Transact-SQL) - SQL Server
Given a valid property_name, if all object types and names are null and a property exists on the current database, that property is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yup, absolutely! I’ll add it in the next release!
I think it’s clear now and we can close it. Maybe it’d be nice to add an exception in the future that’s a bit more clear on what’s going on 😉