RobVious RobVious - 5 months ago 42
C# Question

How to access sql file from Seed() in EF?

I have the following:

string sproc = AppDomain.CurrentDomain.BaseDirectory + "/Stored Procedures/new_message.sql";

However the
path is invalid for some reason. What is the proper way to access files in your project directory from the
Method of EF code-first migrations?

enter image description here


If those .sql files are stored as embedded resources in your assembly, then you can do something like this:

Assembly asy = Assembly.GetExecutingAssembly();

Stream stm = asy.GetManifestResourceStream("yourAssembly.Stored_Procedures.new_message.sql");

if (stm != null)
    string sql = new StreamReader(stm).ReadToEnd();
    // now you have the SQL statements which you can execute 

You may use this to determine the name of the resources from Seed() if needed:

    Assembly asy = Assembly.GetExecutingAssembly();
    string[] names = asy.GetManifestResourceNames();
    throw new Exception(string.Join("", names));