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.

Insert doesn't send pk column

See original GitHub issue

Hi,

After migrating from EF to Linq2db.EntityFramework everything worked as expected, but I wanted to migrate DML also to Linq2Db, so that I could eventually remove EF completely.

From some reason, insert doesn’t work. I have some simple table :

[Table("...", Schema = "...")]
public class NaseljenoMjestoEntity
	{
		[Key]
		public int? NaseljenoMjestoId { get; set; }

		[Required]
		public string Naziv { get; set; }

               ...
        }

I’ve used this code to try to insert:

using (var context = _dbContext.CreateLinqToDbConnection())
			{
			    var result= context.Insert(entity);
			}

but it doesn’t work. Query generated for insert contains all columns except NaseljenoMjestoId, so naturally, it throws Cannot insert the value NULL into column ‘NaseljenoMjestoId’…

NaseljenoMjestoId is Nullable<int>, but changing it to int doesn’t change anything.

We have more than 1000 entities, so I was hoping to reuse EF annotations so that I wouldn’t have to regenerate all the entities (most of them have some kind of custom modifications). It’s ease to replace Key to PrimaryKey, but ForeignKey attribute doesn’t have ‘OtherKey’ part, so it would be tedious to try to manually replace all ForeignKeys to Association.

What am I doing wrong? Is this use case event possible (to create DataConnection from DbContext and do insert, based on EF annotations)?

I’m using linq2db.entityframeworkcore 3.0.0

Also, what is the difference between DataConnection and DataContext? What are different use cases for both of them?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Marka83commented, Apr 21, 2020

I would always trade that for your functionality and simplicity. Keep up with good work.

0reactions
sdanylivcommented, May 21, 2020

Closing for now, will reopen if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

inserting a row having no values for Primary Key columns ...
The problem is that you are not setting a value for the primary key. Contrary to your expectation, primary keys are not automatically ......
Read more >
Insert Does Not Increment Primary Key Correctly
It gives an SQL error that the primary key customer_salesrep_uid is already taken. The application does not seem to know about the inserts...
Read more >
Insert missing SQL Server IDENTITY column values
In this tip we look at how to insert missing values for an identity column using the SET IDENTITY_INSERT command.
Read more >
Primary key violation error when I'm not ...
I need to insert some rows from a temporary table into a persistent table. The persistent table has a primary key composed of...
Read more >
How to insert PK column values into FK column of another ...
Hi, I want to insert tableA P_ID (GUID) values into tableB tableA_Parent column. I couldn 't insert these GIDs into the second table....
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