none
Entity Framework 7 Connection string RRS feed

  • 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?

    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.


    Friday, May 6, 2016 2:25 AM
    Moderator

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
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    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.


    Friday, May 6, 2016 2:25 AM
    Moderator