Geeksltd / Olive

Olive framework, for more productive cross platform .NET solutions. It's available under the GPL v3 license. See License.md for more information.
https://geeksltd.github.io/Olive
Other
44 stars 44 forks source link

Generated SQL file is broken when the combination of Schema and InstanceAccessors is used on Entity declaration #291

Closed m-sadegh-sh closed 3 years ago

m-sadegh-sh commented 3 years ago

I created a brand new project using M# ASP.NET MVC project template. Let's imagine we changed the default ContentBlock entity definition as below:

using MSharp;

namespace Cms
{
    public class ContentBlock : EntityType
    {
        public ContentBlock()
        {
            Schema("Cms"); // I'd like to use a custom schema

            InstanceAccessors("PasswordSuccessfullyReset", "LoginIntro");

            DefaultToString = String("Key")
                .Mandatory()
                .Unique();

            BigString("Content")
                .Mandatory();
        }
    }
}

After successfully building the solution, If we take a look at the generated Cms.ContentBlocks.Create.sql file, we can find the broken line:

-- ContentBlocks Table ========================
IF NOT EXISTS(SELECT * FROM sys.schemas WHERE name = 'Cms')
BEGIN
EXEC('CREATE SCHEMA Cms')
END

CREATE TABLE Cms.ContentBlocks (
    Id uniqueidentifier PRIMARY KEY NONCLUSTERED,
    [Key] nvarchar(200)  NOT NULL,
    Content nvarchar(MAX)  NOT NULL
);

EXEC sp_addextendedproperty @name=N'ReferenceData', @value='Enum', @level0type=N'SCHEMA', @level0name='dbo', @level1type=N'TABLE', @level1name='ContentBlocks'; -- Wrong schema is generated here

You can see that it's going to add an extended property for the ContentBlock table, but the name of the schema isn't valid. So the query execution will fail.

I could find relative information neither on this repo's issues, nor on the docs website, so I decided to open an issue.

hamidmayeli commented 3 years ago

Thank you @m-sadegh-sh,

This issue is related to M# rather than the Olive. I will close it here and will add it to the M# issue tracker.

m-sadegh-sh commented 3 years ago

Thanks, and sorry. I posted it here because I couldn't find M#'s official repo 😁

PaymonK commented 3 years ago

why is the schema name not valid? Is it casing ?

m-sadegh-sh commented 3 years ago

why is the schema name not valid? Is it casing ?

No, it's not related to the casing. It's because the table was created with the correct schema, but sp_addextendedproperty is called with the default dbo schema. Hence I'm not a SQL pro, I don't know what's the purpose of that call, but I checked out other entities, especially those with a similar schema to ContentBlock, and I found no sp_addextendedproperty call in their generated files.