Entity Framework: Error with dynamic connection string

瘦欲@ 提交于 2019-12-24 02:32:30

问题


Cheers all,

I'm using an Entity Framework model. I need to pass my connection string like parameter, so, in another file, I've written (from this question):

namespace MyNamespace.Model
{
    using System;
    using System.Data.Entity;
     using System.Data.Entity.Infrastructure;

    public partial class MyEntities: DbContext
    {
        public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
        {

        }
    }
}

When I startup the app, I call this constructor in this way, so I can refer to this from anyway in the app:

public static MyEntities dbContext = 
                         new MyEntities(mdlImpostazioni.SetConnectionString());

where mdlImpostazioni.SetConnectionString() returns a string (the data are correct):

server=192.168.1.100\SVILUPPO;database=MyDB;uid=myName;pwd=111111;

When I execute this code, it seems to be all ok, but when I try to make a query like:

var query = (from r in MainWindow.dbContext.TabTipoSistema select r);

it throws an exception from here:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

saying:

Additional information: The code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First, make sure that the connection string of Entity Framework is specified in the configuration file of the application running. To use these classes, which were generated from Database First or Model First, with Code First add any additional configuration using attributes or the API DbModelBuilder and remove the code that throws this exception.

the stacktrace is:

in MyNamespace.Model.MyEntities.OnModelCreating(DbModelBuilder modelBuilder) in c:...\Model\Model.Context.cs:row 25 in System.Data.Entity.DbContext.CallOnModelCreating(DbModelBuilder modelBuilder) in System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder() in System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) in System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)*

InnerException is null.

Where is the error?


回答1:


I believe you have to update your App.config section

Not sure but this worked for me as I'm using SQLServer not an local file.

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=MyServer; Integrated Security=True; MultipleActiveResultSets=True" />
  </parameters>
</defaultConnectionFactory>



回答2:


You cannot just use a simple type connectionstring as an entity framework 6 connectionstring through the constructor. I just used the following function to obtain an entity compatible one.

DONT FORGET to change 'YourDBModel' to whatever model you are using.

    /// <summary>
    /// Returns an Entity Connectionstring ready to be used in a EF6 contructor
    /// </summary>
    /// <param name="connectionString">plain connectionstring</param>
    /// <returns>entity connectionstring</returns>
    public string GetEntityConnectionString(string connectionString)
    {
        var entityBuilder = new EntityConnectionStringBuilder();

        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = connectionString;

        // -- WARNING --
        // Check your app config and set the appropriate DBModel (do not just let this be YourDBModel) 
        entityBuilder.Metadata = @"res://*/DB.YourDBModel.csdl|res://*/DB.YourDBModel.ssdl|res://*/DB.YourDBModel.msl";

        var ret = entityBuilder.ToString();

        return ret;
    }

you might also want to add the following to your connectionstring

entityBuilder.ProviderConnectionString = connectionString;  + ";MultipleActiveResultSets=True;App=EntityFramework;";


来源:https://stackoverflow.com/questions/26116697/entity-framework-error-with-dynamic-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!