Page 1 of 1

EF Core Context

Posted: Mon 13 Feb 2017 18:52
by donk
Is there a way to not have the EF Core define the connection string OR have it try to get from a local app settings? I'm using dependency injection and this is being passed in via the option builder parameter.

Basically, I want this line to be commented out when the template generates!

Image
https://www.dropbox.com/s/rlhtx6wikaxhc ... g.png?dl=0

Re: EF Core Context

Posted: Wed 15 Feb 2017 14:02
by Shalex
Please open your *.efml model via Solution Explorer of your Visual Studio, double click the diagram surface to open Model Settings and select the "Use the following connection string from App.Config". Save the model to regenerate the code. Is that what you need?

Code: Select all

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(GetConnectionString("myModelConnectionString"));
        CustomizeConfiguration(ref optionsBuilder);
        base.OnConfiguring(optionsBuilder);
    }

    private static string GetConnectionString(string connectionStringName)
    {
        System.Configuration.ConnectionStringSettings connectionStringSettings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName];
        if (connectionStringSettings == null)
            throw new InvalidOperationException("Connection string \"" + connectionStringName +"\" could not be found in the configuration file.");
        return connectionStringSettings.ConnectionString;
    }

Re: EF Core Context

Posted: Wed 15 Feb 2017 14:51
by donk
No, I don't have an app.settings for this project. I'm injecting the connection string info into this from MVC via the Startup.cs, so I don't want it to know anything about the connection string.

Code: Select all

 // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
	// Add framework services.

	services.AddDbContext<IQContext>(options =>
		options.UseSqlServer(Configuration.GetConnectionString("IqConnectionString")), ServiceLifetime.Scoped);

Re: EF Core Context

Posted: Thu 16 Feb 2017 12:27
by Shalex
Please modify a predefined EF Core template by removing the lines 1017 and 1022.

More information:

Re: EF Core Context

Posted: Thu 16 Feb 2017 14:08
by donk
When I try to make a template into a user template it blows up EntityDeveloper. I have a support ticket in and I haven't gotten any response on it yet :(

Re: EF Core Context

Posted: Thu 16 Feb 2017 14:18
by donk
Anytime I try to generate with a template other than the built-in EF Core, I get this error.

Even without changing the template once I make it into a user template it blows up.

Image

Re: EF Core Context

Posted: Fri 17 Feb 2017 16:09
by Shalex
We have asked for the additional information by email.