Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I first added a migration in visual studio and my code to call in migration is

namespace Tms.Entities.Migrations
{
    using Scripts;
    using System;
    using System.Data.Entity.Migrations;

    public partial class UpdateLocalisationForPortuguese : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Language", "LocalisationId", c => c.Guid());
            CreateIndex("dbo.Language", "LocalisationId");
            AddForeignKey("dbo.Language", "LocalisationId", "dbo.Localisation", "LocalisationId");

            var script = ScriptHelper.LoadMigrationScript("201606220917003_UpdateLocalisationForPortuguese.sql");
            Sql(script);
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.Language", "LocalisationId", "dbo.Localisation");
            DropIndex("dbo.Language", new[] { "LocalisationId" });
            DropColumn("dbo.Language", "LocalisationId");
        }
    }
}


where I am calling a sql script. I have created the sql script and scripted all the queries I needed.

SQL
IF EXISTS (SELECT 1 FROM LANGUAGE WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'aa-ET')
BEGIN
INSERT INTO LOCALISATION VALUES ('1505ba83-a51b-487e-9935-9b1007169136')
INSERT INTO LOCALISEDTEXT VALUES ('1505ba83-a51b-487e-9935-9b1007169136', 'pt-BR', '')
UPDATE LANGUAGE SET LOCALISATIONID = '1505ba83-a51b-487e-9935-9b1007169136' WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'aa-ET'
END

IF EXISTS (SELECT 1 FROM LANGUAGE WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'af-ZA')
BEGIN
INSERT INTO LOCALISATION VALUES ('7774ee36-ff12-438e-8f7c-77f76403b372')
INSERT INTO LOCALISEDTEXT VALUES ('7774ee36-ff12-438e-8f7c-77f76403b372', 'pt-BR', '')
UPDATE LANGUAGE SET LOCALISATIONID = '7774ee36-ff12-438e-8f7c-77f76403b372' WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'af-ZA'
END

IF EXISTS (SELECT 1 FROM LANGUAGE WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'sq-AL')
BEGIN
INSERT INTO LOCALISATION VALUES ('979f748e-cbba-40c6-94cf-469a1b4bb567')
INSERT INTO LOCALISEDTEXT VALUES ('979f748e-cbba-40c6-94cf-469a1b4bb567', 'pt-BR', '')
UPDATE LANGUAGE SET LOCALISATIONID = '979f748e-cbba-40c6-94cf-469a1b4bb567' WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'sq-AL'
END

IF EXISTS (SELECT 1 FROM LANGUAGE WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'am-ET')
BEGIN
INSERT INTO LOCALISATION VALUES ('f420a282-d9c0-4b35-b898-67941f52efc2')
INSERT INTO LOCALISEDTEXT VALUES ('f420a282-d9c0-4b35-b898-67941f52efc2', 'pt-BR', '')
UPDATE LANGUAGE SET LOCALISATIONID = 'f420a282-d9c0-4b35-b898-67941f52efc2' WHERE LOCALISATIONID IS NULL AND CULTURENAME = 'am-ET'
END



When type Update-Database in the package console, it is throwing an error that

Failed to load script 'Tms.Entities.Scripts.Migrations.201606220917003_UpdateLocalisationForPortuguese.sql'


The Scripthelper code is

public static class ScriptHelper
    {
        public static string LoadIndex(string fileName)
        {
            var manifestResourceName = "Tms.Entities.Scripts.Indexes." + fileName;
            return LoadScript(manifestResourceName);
        }

        public static string LoadProcedure(string fileName)
        {
            var manifestResourceName = "Tms.Entities.Scripts.Procedures." + fileName;
            return LoadScript(manifestResourceName);
        }

        public static string LoadMigrationScript(string fileName)
        {
            var manifestResourceName = "Tms.Entities.Scripts.Migrations." + fileName;
            return LoadScript(manifestResourceName);
        }

        public static string LoadScript(string resourceName)
        {
            try
            {
                using (var scriptStream = typeof(ScriptHelper).Assembly.GetManifestResourceStream(resourceName))
                using (var scriptReader = new StreamReader(scriptStream))
                {
                    string result = scriptReader.ReadToEnd();
                    return result;
                }
            }
            catch(Exception ex)
            {
                throw new Exception(string.Format("Failed to load script '{0}'", resourceName), ex);
            }
        }
    }
}

Can anyone please help me to fix this issue.

What I have tried:

I tried to delete the migration and create a new migration and also tried to rebuild the solution. But nothing seems to fix the issue.
Posted
Updated 22-Jun-16 5:50am
v3
Comments
[no name] 22-Jun-16 6:04am    
can you show me the 201606220917003_UpdateLocalisationForPortuguese.sql?
user 3008 22-Jun-16 6:11am    
That is quite big with 1316 lines I will just add few add few lines in that which will be the same for other query with different in culture name and localisation id
Richard Deeming 22-Jun-16 11:09am    
Google doesn't know anything about the ScriptHelper class, or the LoadMigrationScript method.

If that's a custom class that you've written, click "Improve question" and add the code from that class to the question.

If it's a third-party component, you'll need to tell us which one.
user 3008 22-Jun-16 11:52am    
I have included the entire scripthelper custom class that we used for your refernce
Richard Deeming 22-Jun-16 11:57am    
It looks like your .sql file isn't set to be an embedded resource, or is stored in the wrong folder.

Try opening your compiled assembly in a tool like dotPeek[^] and expanding the "Resources" node. Is there an embedded resource with the expected name?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900