Pregunta

I want to add to the UserProfile table of the StarterSite database created by the WebMatrix Starter Site template another column named email_pk (varchar 50) as primary key and add to id a foreign key constraint that references the Email column (varchar 50) of a new tb_contacts table. The latter column isn't a primary key.

When I try to do that manually in the WebMatrix Database workspace I get the following error:

There are no primary or candidate keys in the referenced table 'dbo.UserProfile' that match the referencing column list in the foreign key 'FK_tb_admin_user_UserProfile'. Could not create constraint. See previous errors.

System.Data.SqlClient.SqlException (0x80131904): There are no primary or candidate keys in the referenced table 'dbo.UserProfile' that match the referencing column list in the foreign key 'FK_tb_admin_user_UserProfile'. Could not create constraint. See previous errors. at Microsoft.WebMatrix.DatabaseManager.SqlDatabase.SqlDatabaseProvider.EditTable(String connectionString, String schema, TableInfo tableInfo) at Microsoft.WebMatrix.DatabaseManager.IisDbManagerModuleService.EditTable(DatabaseConnection databaseConnection, String schema, Object tableInfoData, String configPathState) at Microsoft.WebMatrix.DatabaseManager.Client.ClientConnection.EditTable(String schema, Object tableInfoData) at Microsoft.WebMatrix.DatabaseManager.Client.ClientTable.CommitChanges() at Microsoft.WebMatrix.DatabaseManager.Client.TableDesignerViewModel.PerformSave() ClientConnectionId:1da00f40-8f46-4c5b-b423-905c6990fd0d

¿Fue útil?

Solución

If you want to add a foreign key to your UserProfile table, the referenced column must be a primary key or have a unique constraint.

You could manually add a unique constraint with a query like this:

ALTER TABLE tb_contacts
ADD UNIQUE (Email)

By the way, the New Relationship dialog of the WebMatrix Databases workspace displays as possible references only the tables with suitable primary keys.

Note that your StarterSite db alterations inhibit the use of the WebSecurity class.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top