EF Core Context

EF Core Context

Postby donk » Mon 13 Feb 2017 18:52

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/rlhtx6wikaxhc0b/remove-connection-string.png?dl=0
donk
 
Posts: 4
Joined: Thu 26 Jan 2017 14:48

Re: EF Core Context

Postby Shalex » Wed 15 Feb 2017 14:02

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;
    }
Shalex
Devart Team
 
Posts: 7460
Joined: Thu 14 Aug 2008 12:44

Re: EF Core Context

Postby donk » Wed 15 Feb 2017 14:51

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);
donk
 
Posts: 4
Joined: Thu 26 Jan 2017 14:48

Re: EF Core Context

Postby Shalex » Thu 16 Feb 2017 12:27

Please modify a predefined EF Core template by removing the lines 1017 and 1022.

More information:
Shalex
Devart Team
 
Posts: 7460
Joined: Thu 14 Aug 2008 12:44

Re: EF Core Context

Postby donk » Thu 16 Feb 2017 14:08

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 :(
donk
 
Posts: 4
Joined: Thu 26 Jan 2017 14:48

Re: EF Core Context

Postby donk » Thu 16 Feb 2017 14:18

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
donk
 
Posts: 4
Joined: Thu 26 Jan 2017 14:48

Re: EF Core Context

Postby Shalex » Fri 17 Feb 2017 16:09

We have asked for the additional information by email.
Shalex
Devart Team
 
Posts: 7460
Joined: Thu 14 Aug 2008 12:44


Return to Entity Developer