简体   繁体   English

从 SQL 连接创建实体连接时出现问题

[英]Issues while Creating an entity connection from a SQL Connection

We are converting our DB access to use Azure Identity with EF.我们正在转换我们的数据库访问以使用 Azure 身份与 EF。 I tried various different methods and failed because EF is expecting a model to be created and passed into the connection string which i was unable to do with DBConnection.我尝试了各种不同的方法但都失败了,因为 EF 期望创建一个 model 并将其传递到我无法使用 DBConnection 执行的连接字符串中。 Finally I found a way to create the EntityConnection from a SQLConnection so I can retain all the information from the original connection string and add the metadata as needed.最后,我找到了一种从 SQLConnection 创建 EntityConnection 的方法,这样我就可以保留原始连接字符串中的所有信息并根据需要添加元数据。 Here's my code:这是我的代码:

public static EntityConnection GetEntityConnectionString( string efConnectionString, string accessToken )
        {
            MetadataWorkspace workspace = new MetadataWorkspace( new string[] { "res://*/" }, new Assembly[] { Assembly.GetExecutingAssembly() } );
                        
            SqlConnection sqlConnection = new SqlConnection( efConnectionString );
            sqlConnection.AccessToken = accessToken;
            EntityConnection entityConnection = new EntityConnection( workspace, sqlConnection );
            return entityConnection;
        }

When I run this and get to the当我运行它并到达

EntityConnection entityConnection = new EntityConnection( workspace, sqlConnection );

I get the following error:我收到以下错误:

System.ArgumentException: 'MetadataWorkspace must have EdmItemCollection pre-registered.'

Not sure what to do at this point and would really appreciate any insight that can be provided.不确定此时该做什么,非常感谢可以提供的任何见解。

In case anyone else runs into this, here's what I had to do to resolve the issue万一其他人遇到这种情况,这就是我必须做的来解决这个问题

public static EntityConnection GetEntityConnectionString( string efConnectionString, string accessToken )
        {

            MetadataWorkspace workspace = new MetadataWorkspace( new string[] { metadata[0] }, new Assembly[] { Assembly.GetExecutingAssembly() } );

            var edmItemCollection = new EdmItemCollection( "res://*/<filename>.csdl" );
            var store = new StoreItemCollection( "res://*/<filename>.ssdl" );
            var mapping = new StorageMappingItemCollection( edmItemCollection, store, "res://*/<filename>.msl" );


            workspace.RegisterItemCollection( edmItemCollection );
            workspace.RegisterItemCollection( store );
            workspace.RegisterItemCollection( mapping );
            SqlConnection sqlConnection = new SqlConnection( efConnectionString );
            sqlConnection.AccessToken = accessToken;
            EntityConnection entityConnection = new EntityConnection( workspace, sqlConnection );
            return entityConnection;
        }

https://social.msdn.microsoft.com/Forums/en-US/dd7b1c41-e428-4e29-ab83-448d3f529ba4/creating-an-entity-connection-from-a-sql-connection?forum=adodotnetentityframework we can create a new EntityConnection from a MetaDataWorkspace that I then use to construct a DbContext with a different schema https://social.msdn.microsoft.com/Forums/en-US/dd7b1c41-e428-4e29-ab83-448d3f529ba4/creating-an-entity-connection-from-a-sql-connection?forum=adodotnetentityframework我们可以创建来自 MetaDataWorkspace 的新 EntityConnection,然后我用它来构造具有不同模式的 DbContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM