Updatable Primary Key problem


Author
Message
Aaron Young
Aaron Young
Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
I want to set my own value for primary keys but the BO.Save() method doesn't write the primary key back to the database.

For example, I have an Int32 primary key called "CustomerID" and I set the value in SetDefaultValues() and the value is unique. I call BO.Add() which sets the value of CustomerID. I check the value of BO.CustomerID and it is correctly set to a valid value. However, when I do BO.Save() the CustomerID field is not written back to the SQL database (I can see this in SQL Profiler).

I have tried setting PrimaryKeyIsUpdatable to true but I guess I haven't done it correctly as Save() never writes the primary key.

Can anyone help?

Thanks in advance.

Replies
Peter Jones
Peter Jones
Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)Advanced StrataFrame User (718 reputation)
Group: Forum Members
Posts: 386, Visits: 2.1K
Hi Aaron,

As I understand it is the presence of the network card's mac address (on the machine creating the GUID) in the GUID algorithm assures uniqueness.

Peter

Aaron Young
Aaron Young
Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)Advanced StrataFrame User (569 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
I may be wrong but I believe Microsoft switched to version 4 GUIDs from Windows 2000 onwards. This version doesn't use the mac address as it was deemed a security risk given that it allowed the GUID to be traced back to a PC. Version 4 GUIDs uses random parts for all sections.

While I like the idea of sequential GUIDs the problem with UuidCreateSequential is it still uses the mac address and it may only guarantee uniqueness on the local PC if the PC doesn't have a network adapter.

Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Aaron Young (04/26/2008)
While I like the idea of sequential GUIDs the problem with UuidCreateSequential is it still uses the mac address and it may only guarantee uniqueness on the local PC if the PC doesn't have a network adapter.

Hummm,  not NIC, not program to run Tongue

Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
While we are on the subject, does the Database Deployment Toolkit support upgrading of replicated databases?

I have been sick over the weekend and my wife is trying to keep me in bed so I don't get worse...but she had to go to the store BigGrin so I thought I would do a little catching up on the forum.

To answer your question, yes, the DDT will most definitely update a database structure for a SQL Server that uses replication.  When doing this, however, it is best to update the structures when there will be the least amount of activity on the databases (none if possible).  but in any case, you can actually write a program to deploy your structures using the DatbaseMigrator class.  There is a sample that comes with the framework that shows how to do this.  You can create a program that updates all servers on separate threads at the same time...reducing deployment time and making the process more streamlined.  It is the same thing that you would do otherwise, just updating multiple servers on different threads at the same time.

On a separate note here, there has been a lot of discussion on GUIDs and sequential GUIDs.  There is one problem wth sequential GUIDs that exists when you will not be talking to a server and you need to come back in and merge the data.  It is the same problem that exists when using integers.  If you come up with a mechanism to create sequential GUIDs, and you take a laptop, let's say, offsite and it creates records, and there are other laptops doing the same thing, then you can definitly have sync issues.  So this then turns into the situation where each laptop has its own pre-set range to work within, with ends up having the same issues as using a PK.

I recommend doing a little test.  I am of the mind that it is easier to deal with the one outlier that may (though it is extremely unlikely) produce a duplicate GUID.  If you create a program that creates new records non-stop using the System.Guid.NewGuid() for 24-48 hours (this will prouce some SERIOUS records!)  I believe that you will find that there will more than likely never be a duplicate.  If you del with disconnected data (pre-merge or replication) in thi maanner and perform a "pre-merge" query to ensure that there are no duplicates, then you don't get into the syncing issue of sequential GUIDs.  This is just some food for thought.

Paul Chase
Paul Chase
Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)Advanced StrataFrame User (806 reputation)
Group: Forum Members
Posts: 414, Visits: 2.8K
Aaron,

If you have an application that is going to replicate it would need a Nic to be able to do so , but I suppose it is possible that it could be an external type nic that is unplugged whilst the user is adding records. I went back and forth on the what method to use as well, below is some code that uses datetime ticks to sequence a guid.

As far as updating schema, with sql 2005 at least you can make some minor schema changes directly to the publisher. http://msdn2.microsoft.com/en-us/library/ms151870.aspx .

However it is easy to get A.F.U when doing anything major. I have not had to change much except a few alter table's to add a column or 2, but in my next major release I will probably be hating replication due to the number of schema changes I will be making, most likely I'll end up having to drop and re-add subsciptions.

In my case however I can restrict my users from using the software until the database and application has been updated. I create software for internal use only so I have more flexibilty than some when it comes to things like that.

''' <summary>

''' Creates A Seqeuntial Guid based on Date time values

''' </summary>

''' <returns>Sequential Guid</returns>

''' <remarks>converted from c# code found at

''' http://www.informit.com/discussion/index.aspx?postid=a8275a70-0698-46f0-8c8f-bf687464628c

''' I changed it slightly to use utc time due to Pensacola and Ft Walton being Central Time

''' so there would not be a time zone problem </remarks>

Public Shared Function NewSequentialGuid() As Guid

Dim laGuid() As Byte = Guid.NewGuid.ToByteArray

Dim ldBaseDate As DateTime = New DateTime(1899, 1, 1)

Dim ldNow As DateTime = DateTime.UtcNow

' Get the days and milliseconds which will be used to build the byte string

Dim lsdays As TimeSpan = New TimeSpan((ldNow.Ticks - ldBaseDate.Ticks))

Dim lsmsecs As TimeSpan = New TimeSpan((ldNow.Ticks _

- (New DateTime(ldNow.Year, ldNow.Month, ldNow.Day).Ticks)))

' Convert to a byte array

Dim laDays() As Byte = BitConverter.GetBytes(lsdays.Days)

'SQL Server is accurate to 0.003 part of a second

'.NET DateTime ticks are in milliseconds

'so we divide .NET ticks by 3.333333 and should be ok

Dim laSecs() As Byte = BitConverter.GetBytes(CType((lsmsecs.TotalMilliseconds / 3.333333), Long))

'Reverse the bytes to match SQL Servers ordering

Array.Reverse(laDays)

Array.Reverse(laSecs)

' Copy the bytes into the guid

Array.Copy(laDays, (laDays.Length - 2), laGuid, (laGuid.Length - 6), 2)

Array.Copy(laSecs, (laSecs.Length - 4), laGuid, (laGuid.Length - 4), 4)

'Send it back

Return New System.Guid(laGuid)

End Function


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Aaron Young - 17 Years Ago
Aaron Young - 17 Years Ago
Aaron Young - 17 Years Ago
Bill Cunnien - 17 Years Ago
Paul Chase - 17 Years Ago
                         [quote][b]Paul Chase (04/24/2008)[/b][hr] I hope that helps am makes...
Edhy Rijo - 17 Years Ago
                             Hi edhy, Guids as primekeys work fine with SF. The issue I had was...
Paul Chase - 17 Years Ago
                                 [quote]Is there a table or primary key property in the DDT that sets...
Trent L. Taylor - 17 Years Ago
                                 [quote][b]Paul Chase (04/24/2008)[/b][hr]Here issome logicI havein my...
Edhy Rijo - 17 Years Ago
                                     One word of caution, if you are not planning on using replication you...
Trent L. Taylor - 17 Years Ago
                                         Thanks Trent, points taken.:hehe:
Edhy Rijo - 17 Years Ago
Charles R Hankey - 17 Years Ago
Trent L. Taylor - 17 Years Ago
Aaron Young - 17 Years Ago
Trent L. Taylor - 17 Years Ago
                         I think you are right Trent. Whatever way we have tried toengineer it,...
Aaron Young - 17 Years Ago
Edhy Rijo - 17 Years Ago
                         Hi Edhy, That is good to know. We have tested with GUIDs to a limited...
Aaron Young - 17 Years Ago
                             Guys, If youwant to use GUID's then you definately will want to...
Paul Chase - 17 Years Ago
                                 [quote][b]Paul Chase (04/25/2008)[/b][hr]I now use Sqlmerge...
Edhy Rijo - 17 Years Ago
                                     Edhy, Here is a step-by-step on setting up Merge Replication. Just...
Trent L. Taylor - 17 Years Ago
                                         Great! Thanks Trent.
Edhy Rijo - 17 Years Ago
                                             Like Trent said there area tonof articles explaining the different...
Paul Chase - 17 Years Ago
                                                 One consequence of using any form of replication is future changes to...
Aaron Young - 17 Years Ago
                                 Today I started to implement GUID PK for a new SF project, and this...
Edhy Rijo - 17 Years Ago
Peter Jones - 17 Years Ago
Aaron Young - 17 Years Ago
Edhy Rijo - 17 Years Ago
Trent L. Taylor - 17 Years Ago
                         Aaron, If you have an application that is going to replicate it would...
Paul Chase - 17 Years Ago
Aaron Young - 17 Years Ago
Trent L. Taylor - 17 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search