Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 1 day ago Using REDIPS.drag to add drag and drop to your .Net webapplication #li #dib0 http://t.co/n8zY3s7d
about 7 days ago http://t.co/cknQcDbo #Kindle
about 15 days ago Freedom isn't the ability to choose what to do or say, but the ability to choose what not to do or say #freedom
about 29 days ago http://t.co/61KTQknI #Kindle
12 Apr 2012 Force the use of a networking adapter using C# #li #dib0 http://t.co/ZTJOPzOz
9 Apr 2012 Mandriva 2010.2 and USB devices in Virtualbox http://t.co/fwq9gbHB
9 Apr 2012 Execute a http request to you own site with PHP http://t.co/DIvWPrpd
Home Architecture, security and coding Adding data to the database with Visual Studio Data Generation Plan
Adding data to the database with Visual Studio Data Generation Plan
Written by Division by Zero   
Wednesday, 22 February 2012 12:00

An hour ago I needed to use the Data Generation Plan to add generated data to my database. After setting up the generation plan I executed it. I got the following error:

"violation of primary key constraint 'PK_ '.Cannot insert duplicate key in object 'Pk_...'"

Apparently Visual Studio tries to insert the ID's and starts counting with 1. This will work fine on an empty database, but not in this case. I searched a while to find a solution, but couldn't find any. This is what I did to solve the problem (it is a little bit of hacking, but it works):

At first I opened the .dgen file (there is a directory named "Data Generation Plans" in your database solution) with notepad++. The id's look something like this:

<Element Type="ISql100SimpleColumn" Name="[dbo].[Table].[ID]">
    <Property Name="IsNullable" Value="False" />
    <Property Name="IsIdentity" Value="True" />
    <Relationship Name="TypeSpecifier">
        <Entry>
            <Element Type="ISql90TypeSpecifier">
                <Relationship Name="Type">
                    <Entry>
                        <References ExternalSource="BuiltIns" Name="[int]" />
                    </Entry>
                </Relationship>
            </Element>
        </Entry>
    </Relationship>
    <Annotation Type="ColumnConfigAnnotation">
        <Property Name="Selected" Value="True" />
        <Property Name="Seed" Value="5" />
    </Annotation>
</Element>

My first try was to set the IsIdentity property to false. Visual studio still handles the field as a readonly field. So this isn't a solution.

The second try was to remove the IsIdentity property. After saving Visual Studio asked me to update the plan. After the update Visual Studio asked me to update the plan with the latest database properties. Here you will have to decline. If you do this, your changes will be undone.

After this you will be able to set the minimal value for the id-column. Fill in the highest available id from you database +1 and you're almost good to go!

SQL Server will not accept identity insert by default. You must enable this before you run the plan. Another way to do this is to temporarily alter the tables you want to use and disable the Identity Seed. If you run the Generation Plan now, it will run ok.

Of course be very cautious. You are disabling 'safety controls' and you will be able to mess up your database.

 

Add comment


Security code
Refresh

The right word may be effective, but no word was ever as effective as a rightly timed pause. - Mark Twain


© 2009 - 2012, Division by Zero

Template based on the empire template by joomlashack 

Valid XHTML 1.0 Strict  Valid CSS!  Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.