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

Problem

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

code

In the web application, you will 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 configured services for entityframework7 and the connection string using SQL Server.

Attempt

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

I want to write the above code, but its in my main.cs for my console application. I was not successful, obviously, with the following in main.cs:

 SampleConsoleDbContext scab = new SampleConsoleDbContext(); 

I have no way to tell my program that the connection string is, and I still doubt that this is the correct way to get the context created in main.cs

I would be grateful for any help, advice or comments regarding this interesting issue. Thanks.

+5
source share
2 answers

As heavy said that most of this is not needed and is intended only for installing DI, well, just like I read the code, it may be wrong.

Regarding setting the connection string, you can simply:

 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 it as much as you want, but that’s how you pass the connection string to the context instance.

+2
source

Microsoft has actually begun collecting documentation for Entity Framework 7.

From their example, you can simply configure the inline context 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 instantiate 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 ASP.NET examples, calls to .AddEntityFramework() should allow EF to use the same service provider (think dependency injection) as the rest of ASP.NET (meaning that EF will get the same registrars, etc. that ASP.NET uses). But if you do not want to follow this pattern, you can simply use the approach described above.

+7
source

Source: https://habr.com/ru/post/1234660/


All Articles