Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another beginner question on updating data with MM .NET
Message
 
To
27/06/2008 17:32:14
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01327386
Message ID:
01327476
Views:
13
Hi Tim,

As a practice, I never have multiple columns make up a primary key and also never have a primary key have data that can be changed. This practice in fact has no drawbacks (having an additional integer column to me is not a drawback) and many advantages.

The problem with having multiple columns as a PK is that child tables now will need both columns as Foreign keys and makes your joins become cumbersome. If this child table then is a parent table as well, this gets compounded even more.

The problem with having a PK have a value that needs to be changed is that if there are child records you then need to cascade this change to the child records (and grandchild records as well if designed this way).

Every table I create has a PK that has a name of the table followed by the letters 'ID'. I make it an identity type column (int or bigint) and the database assigns the number on an insert.

If you need to have uniqueness on another column(or columns) in the table then you can create a business rule on this as well as set a database constraint by making an alternate key (AK) on these column(s).

So, if changing your database is not a problem, I would suggest changing your table to include a new integer with IDENTITY seed column and make this your Primary Key. Re-running the BLG for this table (and any others that use this methodology) will now not have any problems and you will not have to override anything in the Framework. (BTW, I am assuming your database is SQL Server 2000 or greater).

If changing the database table is a problem and cannot be done, then I can't answer your question about what needs to be overridden.


>Hi again.
>
>I am sure this is covered in the documentation, but I can't seem to see it this afternoon.
>
>I am a simple 2 column child table related to a parent table. The tables primary key is comprised of the 2 columns
>
>Column 1 -> TheFK
>Column 2 -> Name
>
>Primary key -> TheFK+Name
>
>The BLG creates an update SP something like this:
>
>CREATE PROCEDURE [dbo].[LstUpdate]
>(
>	@TheFK int,
>	@Name char(20)
>	
>)
>AS
>	SET NOCOUNT OFF;
>	UPDATE [dbo].[TheTable]
>	SET
>		[Name] = @Name,
>		[TheFK] = @TheFK
>		
>	WHERE
>	(
>		([Name] = @Name) AND
>		([TheFK] = @TheFK)
>		
>	);
>	
>	SELECT
>		[Name],
>		[TheFK]
>	FROM [dbo].[TheTable]
>	WHERE
>		([TheFK] = @TheFK) AND
>		([Name] = @Name)
>
>
>
>Of course this will not work.
>
>The update SP needs to look something like this:
>
>
>
>CREATE PROCEDURE [dbo].[LstUpdate]
>(
>	@TheFK int,
>	@OldName char(20),
>         @NewName char(20)
>	
>)
>AS
>	SET NOCOUNT OFF;
>	UPDATE [dbo].[TheTable]
>	SET
>		[Name] = @NewName,
>		[TheFK] = @TheFK
>		
>	WHERE
>	(
>		([Name] = @OldName) AND
>		([TheFK] = @TheFK)
>		
>	);
>	
>	SELECT
>		[Name],
>		[TheFK]
>	FROM [dbo].[TheTable]
>	WHERE
>		([TheFK] = @TheFK) AND
>		([Name] = @NewName)
>
>
>
>I understand how to fix the procedure and I can modify or override the CreateUpdateCommand factory method. My question is what methods do I need to override so that I can pass the orginal row value for the @OldName parameter?
>
>Thanks for all your help.
>
>Best regards,
>
>Tim Holgerson
Previous
Reply
Map
View

Click here to load this message in the networking platform