question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Key property excluded from SQL

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
artiomchicommented, Oct 22, 2018

Yup, absolutely! I’ll add it in the next release!

0reactions
corstiancommented, Oct 22, 2018

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 😉

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found