Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 705
hlyates Avatar asked Oct 28 '15 04:10

hlyates


1 Answers

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.

like image 76
heavyd Avatar answered Nov 14 '22 22:11

heavyd