Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 14 years ago.
Improve this question
I'm working on better understanding Linq-to-SQL before using on a real project, so I'm playing with it on a little side project. I've a table that I'm trying to update via DataContext.SubmitChanges() and it's failing due to primary key constraints.
I have no control over the table itself (I can't add an actual ID column), so instead I've modified the Linq entity to use 3 columns as a complex key. The inserts are still failing, even though the key data is unique across both the update set and the existing data in the table, and I'm curious to know why. What am I missing that forces Linq to treat these values as non unique?
Note that I am not trying to solve the update problem itself - I've determined that in my situation DataContext.ExecuteCommand() will work just as well. I'm trying to grok where my understanding of Linq (or of my data) is wrong.
The table structure looks like this:
OPRID (char(30))
DATE (DateTime)
PROJECT_ID (char(15))
HOURS (decimal)
The last 3 columns are the complex key.
And here's some sample data, comma delimited:
cschlege, 2009-1-5, 10100, 0.8
cschlege, 2009-1-5, 10088, 1.1
cschlege, 2009-1-5, 10099, 0.8
cschlege, 2009-1-5, 10088, 1.2
The SubmitChanges() consistently fails on the last of those rows, but given a key of DATE, PROJECT_ID, HOURS those rows should be unique. It doesn't fail on the 3rd row, which has a duplication in the HOURS column, but only when it reaches the duplicate PROJECT_ID. If I remove PROJECT_ID from the complex key then the insert fails on the 3rd row, however.
Why is LINQ not treating those rows as uniquely keyed?
Given your explanation I would guess that the HOURS (decimal) is the problem ... It must be mapped somehow with a different precision.
If 1.2 rounds to 1 then you have a problem with:
cschlege, 2009-1-5, 10088, 1 (2)
cschlege, 2009-1-5, 10088, 1 (4)
As you said, if you remove the project ID you end up with failure in the 3rd row:
cschlege, 2009-1-5, 10100, 0 (1)
cschlege, 2009-1-5, 10099, 0 (3)
Can you insert just the last row in the db and then confirm that is with 1.2?
Ah. The answer is that I'm not paying close enough attention to the backing store's rules. There's a different index that's actually failing.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this issue in my c# MVC App where i have a receipt and this receipt has a receipt number and i select the last receipt number inserted in the database and add 1 to it, the problem appears when 2 users submitting the receipt at the same time. What happens is that they both get the same receipt number and they both insert into the database, unfortunately one is inserted and the other gives exception and rollback the whole operation. So my question is there a way to prevent this from happening, in other words reserve a receipt number taken by user, so it won’t be used by the other user.
Thanks in advance.
If using sql server you should probably use an IDENTITY column. Another option would be to use a GUID as the key.
So you need to reset the receipt number for each new year? You could make a separate call to get the receipt number first without doing inserts into other tables. This would reserve a number and return immediately. You could use an identity column for this, either a separate table per year or reset the identity value and truncate at the start of each new year. Or you could have a table for Ids with Year and ReceiptNumber as the PK and insert a new row with incrementing receipt number by one.
This question already has answers here:
Row numbers in query result using Microsoft Access
(7 answers)
Closed 4 years ago.
I am currently working on a C# .Net program. This program is connected to an MS Access database.
I am trying to select just one row from a table by specifying the row number, but I can not find any solution for that.
I hope you can help me with my problem :)
You should use primary key or unique column to get 1 row correct data. Other way is very manually and painfull in my opinion. Example
SELECT * FROM Customer WHERE CustomerID=1;
CostumerID is primary key and it bring 1 row to result. If do you realy want to make it manualy i advise add a datagridview in your project and make its visible to false. Fill your all data into it and get what do you want in it. You can access each row easyly.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I wanna create a web-based bingo game using MVC and EF, I want to store 24 numbers for each card in the database table as a record,I'm not sure how to create my Bingo cards table,one way that comes to my mind is design a table with 24 integer field for each cell's number this way when I fetch the record in business logic I can't traverse the numbers because they are in the separate fields(actually I can but I think this way is not the right way to do)the other way is I concatenate all numbers together and separate it with comma and store it as a string so when I fetch the record I can split the string of comma and have all numbers in an array or something
so what is the best solution for this situation ?
how can I design my table for storing numbers of a bingo card ?
Good question!
You're right, storing it in 24 different integer fields is definitely a bad way to go. Concatenating them with commas might seem like a reasonable compromise, but it's inflexible, as you're treating them as text, not integers, so later on if you wanted to run queries such as SELECT MAX(CardNumber) FROM BingoCardNumbers WHERE CardID = 1 you couldn't do anything like that.
My suggestion would be to have two different tables, BingoCards and BingoCardNumbers. The BingoCards table would have a CardID, as well as other information relevant to the card, such as who the card belongs to, so maybe you'd want a UserID column in there too. No card numbers are stored in this table.
Then, in the BingoCardNumbers table, that would have three columns, CardID, which is the primary key from the first table, NumberIndex (1-24, which is the index of the number in the card), and finally CardNumber, which is the actual number itself. Then, if you want to get all the numbers for a particular card, you can simply do:
SELECT CardNumber FROM BingoCardNumbers WHERE CardID = 1 ORDER BY NumberIndex
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to make AddOrUpdate() generating update t-sql?
How to make AddOrUpdate() generating t-sql sent to sql server in sequence?
How to create multiple columns 'reference' index in EF6?
come on
This is to gather all the solutions of pitfall I encountered on initializing
System.Data.Entity.Migrations.DbMigrationsConfiguration.Seed().
I am T-SQL master, but I'm not EF master due to I always on the MS SQL side.
I found DbContext.AddOrUpdate(new instance(){}) didn't generate the update T-sql, and throw exception when EF encountered inserting the repeat row error in sql server.
Here is the solution gave by roelant-m:
migrationdbsett addorupdate trouble
When I solved the AddOrUpdate(new Instance(){}) inserting same row problem, I encountered the EF optimizing problem, yeah, he is so smart that optimize all the AddOrUpdate( new instance()) in method Seed(). and then he throw exceptions.
Due to the Initializing database must be in sequence (some table has referenced the other tables.)
Here is the solution gave by endi zhupani:
how to make the addorupdate run in order in dbmigrationconfiguration derived
When I solved the initializing AddOrUpdate(new Instance(){}) in sequence, I encountered the multiple column 'references' index, some element of the index are navigation property.
Here is solution gave by sampath:
how to create multi columns reference index join index in csharp model
This question already has answers here:
Identity increment is jumping in SQL Server database
(6 answers)
Closed 6 years ago.
I have a table in EntityFramework that has a field named by ID,
this field is primary key and is Identity.
when i add records into this table, this field value increases per recor, after adding several records, this value suddenly increases
For example, increased from 90 in 1010
While no transaction has been unsuccessful.
what is the problem?
If you are using Azure SQL this can just happen. We had it happen a few times. It is just the nature of how Azure sql works.
See this question Windows Azure SQL Database - Identity Auto increment column skips values it goes into a detailed explanation for a case very similar to yours