I created a models using code-first and without storing password in App.config, I passing it in constructor of DbContext instead:
public TasksWithoutPasswordContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
this.Database.Connection.ConnectionString = nameOrConnectionString;
}
But I got a runtime error while trying to get DbSet of my DbContext:
using (TasksWithoutPasswordContext contextWithoutPassword = new TasksWithoutPasswordContext(connectionString))
{
foreach (var user in contextWithoutPassword.users)
{
MessageBox.Show(user.user);
}
}
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).
I tried to mark my class as next:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class TasksWithoutPasswordContext : DbContext
and it didn't helped.
As you already found the solution yourself, I add the answer:
When you're inheriting from DbContext and call the base constructor, you could use the default-base-constructor (the parameterless one) and set the connectionstring in constructor:
public TasksWithoutPasswordContext(string nameOrConnectionString) : base()
{
this.Database.Connection.ConnectionString = nameOrConnectionString;
}
Related
I'm trying to connect to the local database instance that visual studios provides, I setup a connection string in the appsettings.json however, it keeps failing at the integrated security keyword, it says it doesn't exist. However if I take it out, the response gets a timeout because there was no pre-authorization handshake which is because I set my dbcontext to identitydbcontext so I would need to specify the integrated security = true keyword but alas I'm stuck
This is the default connection string:
"DefaultConnection":"Server=(localdb)\\\\
mssqllocaldb;Database=ChatAppTr;MultipleActiveResultSets=true;
Integrated Security=True;"
This is how I setup the database context for my web app:
///Specify what type of user you want in identity context:
public class AppDbContext : IdentityDbContext<User>
{
public AppDbContext(DbContextOptions<AppDbContext> options) :
base( options){ }
/// Registers models to AppdbContext
public DbSet<Chat> Chats { get; set; }
public DbSet<Messages> Messages { get; set; }
}
}
I expected it to just connect to the localdb instance but nothing happened.
i have a win-form app that uses CodeFirst to work with data.
after creating database from my models i have imported records to it from another database.it works fine in my PC but when i deploy my app to another pc, codefirst tries to create new database and throws this error :
System.Data.Entity.Core.EntityException: The underlying provider failed on Open. --->
System.Data.SqlClient.SqlException: Cannot create file
'E:\New folder (2)\Release\MyDB_log.ldf' because it already exists.
Change the file path or the file name, and retry the operation.
Could not open new database 'MyDB'. CREATE DATABASE is aborted.
Cannot attach the file 'E:\New folder (2)\Release\MyDB.mdf' as database 'MyDB'.
connection string :
<connectionStrings>
<add name="AppDbContext"
connectionString="Server=.\sqlexpress;AttachDbFilename=|DataDirectory|\MyDB.mdf;Database=MyDB;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
</connectionStrings>
AppDbContext :
public class AppDbContext :DbContext
{
static AppDbContext() {
Database.SetInitializer<AppDbContext>(null);
}
public DbSet<Stock> Stocks { get; set; }
public DbSet<Report> Reports { get; set; }
}
Migration class:
internal sealed class Configuration : DbMigrationsConfiguration<Sita_Election.DAL.AppDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "Sita_Election.DAL.AppDbContext";
AutomaticMigrationDataLossAllowed = false;
}
protected override void Seed(Sita_Election.DAL.AppDbContext context)
{
}
}
so i want to use existing database that is created in development with codefirst,how can i do this ?
Is there any reason to have a database per application?
I would suggest using just a SQL DB if possible.
Thanks,
Phill
Your connection string is using a trusted connection so your IIS user is going to need access to that file or you could switch to a sql login. http://th2tran.blogspot.com/2009/06/underlying-provider-failed-on-open.html
I want to connect my project to an Sqlite database. So, in my Database Context I have this cunstructor and an Initialize method
private DatabaseContext(string cnxString) : base(cnxString)
{
Database.SetInitializer<DatabaseContext>(new DatabaseInitialiser());
}
public static void initialize(string connx)
{
_instance = new DatabaseContext(connx);
}
and In the program.cs I call Inialize method to set the connexion string
DatabaseContext.initialize("data source=D:\storage.sqlite;");
So, every user can connect to his database by passing his connexion string.
But, when running my application I have this error :
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
You'll need to pass in a SQLite connection object to the base constructor:
private DatabaseContext(string cnxString)
: base(new SQLiteConnection(cnxString), contextOwnsConnection: true)
{
}
I've spent a while on this now and have only found a workaround solution that I'd rather not do...
I have a context as shown below. Is there a programmatic way to specify the database to connect to via the constructor, and get it to use the System.Data.SQLite entity framework provider to connect to a SQLite database? This is working via the app.config (with a connectionstring called "Context"), but not via any programmatic way I can find of supplying it. I have tried using an entity connectionstring builder and that produces the following string:
provider=System.Data.SQLite;provider connection string='data
source="datafile.db"'
When the context is first queried with this string I get a message "Keyword not supported: 'provider'."
public class Context : DbContext
{
public IDbSet<Test> Tests { get; set; }
public Context()
: base("Context")
{
}
}
*Edit.
I may have solved this by implementing my own connectionfactory:
public class ConnectionFactory : IDbConnectionFactory
{
public DbConnection CreateConnection(string nameOrConnectionString)
{
return new SQLiteConnection(nameOrConnectionString);
}
}
Then setting:
Database.DefaultConnectionFactory = new ConnectionFactory();
However, it seems like there should be a built in way to do this, and also one that does not involve overriding the global default connection factory.
I used SQL Server CE 4.0 in my windows app and use Entity Framework to create a model of it.
It works fine but my problems is that it doesn't have a constructor to change the connection string, and by default it reads the connection string from the app.config file.
using (var Context = new MyEntitiesModel(//has no constructor))
{
...
}
I create a dynamic connection string and
using (var Context = new MyEntitiesModel())
{
Context.Database.Connection.ConnectionString = entityConnection.ConnectionString;
}
It works fine by this way but if I remove another connection string in app.config file it gave me this.
error = invalid metasource ....
because the default constructor uses it
How can I handle it?
Create your own constructor. MyEntitiesModel is partial class you can add your own partial part of the class and add constructor accepting a connection string.
public partial class MyEntitiesModel {
public MyEntitiesModel(string connectionString) : base(connectionString) { }
}
Im using DbContext. There are several Overload Constructors eg:
ObjectContext also has a similar set of constructor overloads.
System.Data.Entity DbContext example
Context = new BosMasterEntities(nameOrConnectionString: nameOrConnectionString);
You can connect to multiple Dbs at same time.