How to use EF7 and connect to SQL Server without startup.cs in ASP.NET 5 Beta 8 console application?

asp.net-core asp.net-core-mvc c# entity-framework-core

Question

Problem

I am writing an asp.net 5 console sample application and I want to use Entity Framework 7 to talk to my backend. I know how to do this in an web application, but I am lost for how to accomplish this for a console application when not using startup.cs but main.cs.

Code

In a web application, you would have the following code in startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNet5;Trusted_Connection=True;";

    services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
}

Here you have services being configured for entityframework7 and the connection string that is using SQL Server.

Attempt

I have looked on GitHub, Google, and Bing but only found sample projects and code for web applications with EF7. I have not found a documentation that discusses EF7 with the console application.

I want to write the above code, but have it in my main.cs for my console application. I have not had success, obviously with the following in main.cs:

SampleConsoleDbContext scab = new SampleConsoleDbContext();

I have no way of telling my program what the connection string and I still have doubts this it the right way to get the context instantiated in main.cs as well.

I would appreciate any assistance, advice, or comments regarding this interesting problem. Thank you.

Accepted Answer

As heavyd said most of that is not needed and is just for setting up DI, well thats also how i read the code, could be wrong.

As for setting the connection string you could simple do:

var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNet5;Trusted_Connection=True;";

SampleConsoleDbContext scab = new SampleConsoleDbContext(connection);

pass the connection string to the context.

you can abstract this as much as you like, but this is how you would pass the connection string to the instance of the context.


Popular Answer

Microsoft has actually started to build the documentation for Entity Framework 7.

From their sample you can just do configuration of the context inline by overriding the OnConfiguring method:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        builder.UseSqlServer(@"<connection string>");
    }
}

And then you can just create instances of your context in Main:

class Program
{
    static void Main(string[] args)
    {
        using (var db = new BloggingContext())
        {
            db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
            var count = db.SaveChanges();
            Console.WriteLine("{0} records saved to database", count);

            Console.WriteLine();
            Console.WriteLine("All blogs in database:");
            foreach (var blog in db.Blogs)
            {
                Console.WriteLine(" - {0}", blog.Url);
            }
        }
    }
}

In the ASP.NET examples, the calls to .AddEntityFramework() is to allow EF to use the same Service Provider (think dependency injection) as the rest of ASP.NET (this means EF will get the same loggers etc. that ASP.NET is using). But if you don't want to follow that pattern then you can just use the approach shown above.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why