Answered by:
Entity Framework 7 Connection string

Question
-
This is how i made connection to database in EF6:
Form1.context = new PostgresContext(new NpgsqlConnection(Configuration.connectionString));
How Can i do it in EF 7? Pass Connection object or connection string to dbcontext?
- Moved by KareninstructorMVP Thursday, May 5, 2016 12:34 PM Moved from C# forum
Thursday, May 5, 2016 12:30 PM
Answers
-
Hi aivarinns,
>>How Can i do it in EF 7? Pass Connection object or connection string to dbcontext?
As far as I know, EF7 doesn't support Connection object and connection string with Dbcontext Constructor. you could use
DbContextOptions
or ServiceProvider to connect your database.#
DbContextOptions
public class BloggingContext : DbContext { public BloggingContext(DbContextOptions options) : base(options) { } public DbSet<Blog> Blogs { get; set; } }
#ServiceProvider
using (var context = serviceProvider.GetService<BloggingContext>()) { // do stuff }
For more information about Configuring a DbContext, please refer to:
http://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html#more-reading
Best regards,
Cole Wu
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Zhanglong WuMicrosoft contingent staff Tuesday, May 10, 2016 4:24 AM
- Marked as answer by Herro wongMicrosoft contingent staff Friday, May 13, 2016 1:26 AM
- Edited by Zhanglong WuMicrosoft contingent staff Tuesday, May 31, 2016 2:36 AM
Friday, May 6, 2016 2:25 AM
All replies
-
Hello,
Since this is a EF question I am moving your question to the EF forum.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Thursday, May 5, 2016 12:34 PM -
Hi aivarinns,
>>How Can i do it in EF 7? Pass Connection object or connection string to dbcontext?
As far as I know, EF7 doesn't support Connection object and connection string with Dbcontext Constructor. you could use
DbContextOptions
or ServiceProvider to connect your database.#
DbContextOptions
public class BloggingContext : DbContext { public BloggingContext(DbContextOptions options) : base(options) { } public DbSet<Blog> Blogs { get; set; } }
#ServiceProvider
using (var context = serviceProvider.GetService<BloggingContext>()) { // do stuff }
For more information about Configuring a DbContext, please refer to:
http://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html#more-reading
Best regards,
Cole Wu
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Zhanglong WuMicrosoft contingent staff Tuesday, May 10, 2016 4:24 AM
- Marked as answer by Herro wongMicrosoft contingent staff Friday, May 13, 2016 1:26 AM
- Edited by Zhanglong WuMicrosoft contingent staff Tuesday, May 31, 2016 2:36 AM
Friday, May 6, 2016 2:25 AM