timabell / ef-enum-to-lookup

Generates lookup tables from enum references in Microsoft Entity Framework 6.1
https://www.nuget.org/packages/ef-enum-to-lookup
69 stars 29 forks source link

Always add schema to SQL statements when creating mapping tables #44

Open Choc13 opened 8 years ago

Choc13 commented 8 years ago

Currently the script that is generated looks something like this:

IF OBJECT_ID('Enum_MyEnum', 'U') IS NULL
begin
    CREATE TABLE [Enum_MyEnum] (Id int CONSTRAINT PK_Enum_MyEnum PRIMARY KEY, Name nvarchar(255));
    exec sys.sp_addextendedproperty @name=N'MS_Description', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE',
        @level1name=N'Enum_MyEnum', @value=N'Automatically generated. Contents will be overwritten on app startup. Table & contents generated by https://github.com/timabell/ef-enum-to-lookup';
end

However if the database is not using dbo as the default schema then the exec sys.sp_addextendedproperty statement fails with the error:

Msg 15135, Level 16, State 8, Procedure sp_addextendedproperty, Line 37

Object is invalid. Extended properties are not permitted on 'dbo.Enum_MyEnum', or the object does not exist.

This is because the table Enum_MyEnum does not exist as dbo.Enum_MyEnum. Please could the generated SQL be changed to always explicitly include the schema name, such as:

IF OBJECT_ID('dbo.Enum_MyEnum', 'U') IS NULL
begin
    CREATE TABLE [dbo.Enum_MyEnum] (Id int CONSTRAINT PK_Enum_MyEnum PRIMARY KEY, Name nvarchar(255));
    exec sys.sp_addextendedproperty @name=N'MS_Description', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE',
        @level1name=N'Enum_MyEnum', @value=N'Automatically generated. Contents will be overwritten on app startup. Table & contents generated by https://github.com/timabell/ef-enum-to-lookup';
end
timabell commented 8 years ago

feel free to submit a PR, though make sure you understand the various branches first

see also issue #1