ef-dynamic-schema-changes-using-database-first

前端之家收集整理的这篇文章主要介绍了ef-dynamic-schema-changes-using-database-first前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://chriseelmaa.com/entity...


internal static class DatabaseUtils
{
    /// <summary>
    /// Builds the connection string for Entity framework.
    /// </summary>
    /// <returns></returns>
    public static EntityConnection BuildConnection()
    {
        var config = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"];

        var builder = new EntityConnectionStringBuilder();
        builder.Provider = config.ProviderName; //Oracle.ManagedDataAccess.Client
        builder.ProviderConnectionString = config.ConnectionString;
        
        //注意大写
        string schema = Regex.Match(config.ConnectionString,@"(?<=User ID=)[^;]+",RegexOptions.IgnoreCase).Value;

        return CreateConnection(schema,builder,"Model");
    }

    /// <summary>
    /// Creates the EntityConnection,based on new schema & existing connectionString
    /// </summary>
    /// <param name="schemaName">Name of the schema.</param>
    /// <param name="connectionBuilder"></param>
    /// <param name="modelName">Name of the model.</param>
    /// <returns></returns>
    private static EntityConnection CreateConnection(string schemaName,EntityConnectionStringBuilder connectionBuilder,string modelName)
    {
        Func<string,Stream> generateStream =
            extension => Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Concat(modelName,extension));

        Action<IEnumerable<Stream>> disposeCollection = streams =>
        {
            if (streams == null)
                return;

            foreach (var stream in streams.Where(stream => stream != null))
                stream.Dispose();
        };

        var conceptualReader = generateStream(".csdl");
        var mappingReader = generateStream(".msl");
        var storageReader = generateStream(".ssdl");

        if (conceptualReader == null || mappingReader == null || storageReader == null)
        {
            disposeCollection(new[] { conceptualReader,mappingReader,storageReader });
            return null;
        }

        var storageXml = XElement.Load(storageReader);

        foreach (var entitySet in storageXml.Descendants())
        {
            var schemaAttribute = entitySet.Attributes("Schema").FirstOrDefault();
            if (schemaAttribute != null)
                schemaAttribute.SetValue(schemaName);
        }

        storageXml.CreateReader();

        var workspace = new MetadataWorkspace();

        var storageCollection = new StoreItemCollection(new[] { storageXml.CreateReader() });
        var conceptualCollection = new EdmItemCollection(new[] { XmlReader.Create(conceptualReader) });
        var mappingCollection = new StorageMappingItemCollection(conceptualCollection,storageCollection,new[] { XmlReader.Create(mappingReader) });

        workspace.RegisterItemCollection(conceptualCollection);
        workspace.RegisterItemCollection(storageCollection);
        workspace.RegisterItemCollection(mappingCollection);

        var connection = DbProviderFactories.GetFactory(connectionBuilder.Provider).CreateConnection();
        if (connection == null)
        {
            disposeCollection(new[] { conceptualReader,storageReader });
            return null;
        }

        connection.ConnectionString = connectionBuilder.ProviderConnectionString;
        return new EntityConnection(workspace,connection);
    }
}

猜你在找的Oracle相关文章