Initially in the entity structure code, why the key is not unique

I used the methods mentioned here to create composite keys. SQL Server calls it PrimaryKey, but it is not unique (!). Is there a way to specify uniqueness in attributes or a free API? I found several hacks here , but it should be possible to do this in advance ...

Non-unique value combinations are the result of an SQLBulkCopy operation. Is this possible?

[edit] my assumptions were wrong - read my answer.

+3
source share
2 answers

My mistake! The keys are unique, including compound keys. My problem was matching the columns of the SqlBulCopy class. I was doing

Public Sub DoBulKCopy(dt As DataTable, cns As String)
    Dim cn As New SqlConnection(cns)
    cn.Open()
    Dim copy As New SqlBulkCopy(cn)
    For i As Integer = 0 To dt.Columns.Count - 1
        copy.ColumnMappings.Add(i,i)
    Next

Public Sub DoBulKCopy(dt As DataTable, cns As String)
    Dim cn As New SqlConnection(cns)
    cn.Open()
    Dim copy As New SqlBulkCopy(cn)
    For i As Integer = 0 To dt.Columns.Count - 1
        copy.ColumnMappings.Add(dt.Columns(i).ColumnName, dt.Columns(i).ColumnName)
    Next

, .

+2

- . , .

+1

Source: https://habr.com/ru/post/1791933/


All Articles