Open kkkmail opened 4 years ago
@kkkmail thanks for the bug report.
this is due to the default constraint on the column:
According to the comment, there is a backing for this behaviour being the way it is.
If you alter the constraint to not have a default value:
ALTER TABLE [dbo].[EFTests] ADD [StateCode] [nvarchar](2) not null
Then it will work as expected.
I'd like to consider that default value on column that is part of primary key is double design smell, you can't update a primary key column and it is reasonable to expect insert statements to specify the value.
If you want to insert a default value just for the time of creating the new column, you should drop the default constraint and specify the value being not null.
Please let me know if you agree to close this by design and if the work around is good enough?
code to reproduce in a script:
#r "System.Data"
#r "nuget: FSharp.Data.SqlClient"
[<Literal>]
let connectionString = "Data Source=.;Initial Catalog=testtp;Integrated Security=True;"
open FSharp.Data
open System.Data.SqlClient
type RatingDB = SqlProgrammabilityProvider<connectionString>
type TestTbl = RatingDB.dbo.Tables.EFTests
type TestTblRow = TestTbl.Row
type TruncateTestTbl = SqlCommandProvider<"truncate table dbo.EFTests", connectionString>
type ZipCodeCityInfo =
{
ZipCode : string
State : string
City : string
CityOriginalName : string
}
let data =
[
{
ZipCode = "12345"
State = "MA"
City = "ABCDEFGH"
CityOriginalName = "ABCDEFGH"
}
{
ZipCode = "12345"
State = "CT"
City = "ABCDEFGH"
CityOriginalName = "ABCDEFGH"
}
]
let updateTbl (t : TestTbl) (r : ZipCodeCityInfo) =
let newRow =
t.NewRow(
City = r.City,
StateCode = r.State,
ZipCode = r.ZipCode
)
newRow.CityOriginalName <- r.CityOriginalName
t.Rows.Add newRow
let conn = connectionString
do
use truncateTbl = new TruncateTestTbl(conn, commandTimeout = 3600)
truncateTbl.Execute() |> ignore
use tbl = new TestTbl()
data |> List.map (fun e -> updateTbl tbl e) |> ignore
use bulkCopy = new SqlBulkCopy(conn, BulkCopyTimeout = 3600, DestinationTableName = "EFTests");
bulkCopy.WriteToServer (tbl) |> ignore
Description
After extending multi column PK, bulk insert with the data different only in the new column fails with System.Data.ConstraintException.
Repro steps
Create table
EFTests
:Modify table:
Attempt to run the test (adjust
type RatingDB
for your local environment,RatingSqlProviderName
is just a const string). The set up is:and the test is (adjust inheritance / code to get to SQL connection -
use conn = ...
):The
__________
contain irrelevant proprietary information.Note that
StateCode
is missing in the error message.Expected behavior
Observe that if the table is created in one step:
then the test will pass.
Actual behavior
The test fails.
Known workarounds
Drop the table and recreate it in one step. This is often not an option.
Related information