Visual Studio 2010 Database Project - Unresolved R

2019-01-22 21:07发布

问题:

We have a couple of synonyms that are being used to reference tables in a separate database (that's on the same server).

The actual synonyms are fine, but the stored procs/user defined functions that reference said synonyms show the following error:

Error   13  SQL03006: Column: [dbo].[GetCocosIndexSearched].[User ID] contains an unresolved reference to an object. Either the object does not exist or the reference is ambiguous because it could refer to any of the following objects: [dbo].[AuditType].[e]::[LOGIN_ID], [dbo].[EMPLOYEES].[e]::[LOGIN_ID], [dbo].[EMPLOYEES].[LOGIN_ID] or [dbo].[SearchCIBirthsRequest].[e]::[LOGIN_ID].    C:\VSTS\Corporate\WARS Audit\MAIN\Source\RBDM.Audit.Database\Schema Objects\Schemas\dbo\Programmability\Functions\GetCocosIndexSearched_1.function.sql  21  5   RBDM.Audit.Database

[dbo].[GetCocosIndexSearched] is a function and [dbo].[EMPLOYEES] is the synonym.

These all work fine in the actual database itself - the problem lies solely with the database project. I've tried adding a reference to the separate database (either through a new database project or a database project schema), but then have conflict issues with the synonym and reference table:

Error   7   SQL04105: The model already has an element that has the same name dbo.EMPLOYEES.    C:\VSTS\Corporate\WARS Audit\MAIN\Source\RBDM.Audit.Database\Schema Objects\Schemas\dbo\Synonyms\EMPLOYEES.synonym.sql  2   1   RBDM.Audit.Database

Any ideas? There's a workaround mentioned in another stackoverflow post that talks about placing the creation scripts in Script.PostDeployment.sql - sounds a bit too hacky to me.

回答1:

This happens because in the caller database you must add a reference to the dependent database and specify also a value for the database name. Look below:

Then you can create your synonym using the following code:

CREATE SYNONYM [dbo].[mytable] FOR [$(MasterDatabase)].[dbo].[mytable]


回答2:

I have the same problem with table synonyms that point to another database on the same server as the main database. Adding the reference works for almost everything but in-line table-valued functions. As soon as you try to use a synonym

create synonym syn.Table for [$(MyDb)].[dbo].[Table];

in such a function the error is back. SSDT 2013 Update 4 is my version. It's just unbelievable that in 2016 such an error is still with us. Handling synonyms should be seamless as this is the prime mechanism for abstracting dependencies. Can't believe it's still not been corrected.