如何在EF迁移中使用SqlResource方法?

[英]How do I use the SqlResource method in EF Migrations?


MSDN says this method "Adds an operation to execute a SQL resource file". Its signature is:

MSDN说这个方法“添加一个执行SQL资源文件的操作”。它的签名是:

protected internal void SqlResource(
    string sqlResource,
    Assembly resourceAssembly = null,
    bool suppressTransaction = false,
    object anonymousArguments = null
)

And the sqlResource parameter is described as The manifest resource name of the SQL resource file to be executed. Is a "SQL resource file" the same as a normal .resx resource file, and if so, it can contain many files, so how do I specify the name of the resource file, and the file within that resource, in this one parameter? Or is a "SQL resource file" a different type of file, that only contains one SQL script, and I just pass the name of that file for the sqlResource parameter?

并且sqlResource参数被描述为要执行的SQL资源文件的清单资源名称。 “SQL资源文件”是否与普通的.resx资源文件相同,如果是这样,它可以包含许多文件,那么如何在这一个参数中指定资源文件的名称以及该资源中的文件?或者是“SQL资源文件”是一种不同类型的文件,只包含一个SQL脚本,我只是为sqlResource参数传递该文件的名称?

1 个解决方案

#1


8  

Since EF is an open source framework, the easiest way to find this out is look at source code:

由于EF是一个开源框架,最简单的方法是查看源代码:

protected internal void SqlResource(string sqlResource, Assembly resourceAssembly = null, bool suppressTransaction = false, object anonymousArguments = null)
{
    Check.NotEmpty(sqlResource, "sqlResource");

    resourceAssembly = resourceAssembly ?? Assembly.GetCallingAssembly();

    if (!resourceAssembly.GetManifestResourceNames().Contains(sqlResource))
    {
            throw new ArgumentException(Strings.UnableToLoadEmbeddedResource(resourceAssembly.FullName, sqlResource));
    }

    using (var textStream = new StreamReader(resourceAssembly.GetManifestResourceStream(sqlResource)))
    {
        AddOperation(
            new SqlOperation(textStream.ReadToEnd(), anonymousArguments)
                {
                    SuppressTransaction = suppressTransaction
                });
    }
}

You can see here that this method first reads manifest resource with the name sqlResource from assembly resourceAssembly (if null - it uses calling assembly). Then text from this resource is treated as sql and regular SqlOperation is added with it and parameters you provide.

您可以在此处看到此方法首先从程序集resourceAssembly中读取名为sqlResource的清单资源(如果为null - 它使用调用程序集)。然后将来自此资源的文本视为sql,并添加常规SqlOperation及其提供的参数。

There are many ways to embed arbitrary file as mantifest resource into your assembly. Easy way for such a particular situation is to just add this file to the project with build action "Embedded Resource". So you have sql file named something like "CreateTables.sql". Right click on your Visual Studio project, click "Add > Existing Item", choose your "CreateTables.sql" file, then right click on it in Visual Studio project and choose Build Action as "Embedded Resource". This will result in manifest resource with name DefaultNamespaceOfYourAssembly.Folder.SubFolder.CreateTables.sql (if you put it in the root, without folders, then obviously skip Folder.SubFolder part), which you then can use as sqlResource in the method above.

有许多方法可以将任意文件作为mantifest资源嵌入到程序集中。这种特殊情况的简单方法是使用构建操作“嵌入式资源”将此文件添加到项目中。所以你有一个名为“CreateTables.sql”的sql文件。右键单击Visual Studio项目,单击“添加>现有项”,选择“CreateTables.sql”文件,然后在Visual Studio项目中右键单击它,并选择“构建操作”作为“嵌入式资源”。这将导致名为DefaultNamespaceOfYourAssembly.Folder.SubFolder.CreateTables.sql的清单资源(如果您将其放在根目录中,没有文件夹,那么显然会跳过Folder.SubFolder部分),然后您可以在上面的方法中将其用作sqlResource。

As for resx file you mentioned - that file itself is manifest resource, but it cannot be used here, because you cannot use just a part of it (so some resource with the given key) - you can only use it as a whole, but it's xml file and not sql.

至于你提到的resx文件 - 该文件本身是清单资源,但它不能在这里使用,因为你不能只使用它的一部分(所以一些资源与给定的密钥) - 你只能将它作为一个整体使用,但是它是xml文件而不是sql。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/11/24/758578fa09087cf2f42e7c39c5af2db5.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号