in App.Config file :
<connectionStrings>
<add name="DB_PhonebookEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=DB_Phonebook;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
in MyModel.Designer.cs file :
namespace SimplePhoneBook
{
public partial class DB_PhonebookEntities : ObjectContext
{
public DB_PhonebookEntities() : base("name=DB_PhonebookEntities", "DB_PhonebookEntities")
{
....
}
public DB_PhonebookEntities(string connectionString): base(connectionString, "DB_PhonebookEntities")
{
....
}
public DB_PhonebookEntities(EntityConnection connection) : base(connection, "DB_PhonebookEntities")
{
....
}
....
}
How can I set Application.StartupPath into my ConnectionString?
I want create "Model" from my database thais on the applicatin path(...\debug\bin\mydatabase.mdf)
and using entities in my model!
Append this to your connection string. This will point to WebApplication1\App_Data. So your mdf file should be in App_Data folder
AttachDBFilename=|DataDirectory|\aspnet-WebApplication1-20141203171438.mdf
Related
Am using EF CodeFirst (Version 6.1.3) with ASP.Net MVC5 / Sql 2014 Express.
It's Entity Framework code first, setup to talk to a local sql server instance using trusted authentication, however database does not get created.
connection string in the Web.config file is
<connectionStrings>
<add name="Ppa" providerName="System.Data.EntityClient" connectionString="Server=localhost;Database=PpaOnline;Trusted_Connection=True;" />
</connectionStrings>
connection string in the App.config file of DAL layer is:
<connectionStrings>
<add name="Ppa" providerName="System.Data.EntityClient" connectionString="Server=localhost;Database=PpaOnline;Trusted_Connection=True;" />
</connectionStrings>
DbContext class is:
public class Db : DbContext
{
static Db()
{
var type = typeof(SqlProviderServices);
if (type == null)
throw new Exception("Do not remove, ensures static reference to System.Data.Entity.SqlServer");
}
public Db()
{
Configuration.LazyLoadingEnabled = false;
}
public DbSet<Company> Companies { get; set; }
public DbSet<User> Users { get; set; }
}
The PpaOnlineConfiguration class is:
public class PpaOnlineConfiguration : DbConfiguration
{
public PpaOnlineConfiguration()
{
SetDefaultConnectionFactory(new SqlConnectionFactory("Server=localhost;Database=PpaOnline;Trusted_Connection=True;"));
}
}
Kindly provide input, as to where am I going wrong.
Thanks,
I am having 1 class library named as AuxiliaryLib which contains app settings like below:
<appSettings>
<add key="connectionstring" value="Data Source=My-Pc;Initial Catalog=Db1;User ID=StackOverflow;Password=123456"></add>
</appSettings>
Now, in this class library I have 1 class file which return connection string like below:
public static string ReturnConnectionString()
{
return System.Configuration.ConfigurationManager.AppSettings["connectionstring"];
}
I have 1 wcf project in which i have given reference of above class library i.e AuxiliaryLib and calling above connection string method but I am getting null.
Public void MyMethod()
{
var connectionRepo = new ConnectionRepo();
string str = ConnectionRepo.ReturnConnectionString(); //getting null
}
I have web.config in wcf project where I have added connection string but still getting null:
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="connectionstring" value="Data Source=My-Pc;Initial Catalog=Db1;User ID=StackOverflow;Password=123456"></add>
</appSettings>
<!--<connectionStrings>
<add name="connectionstring" connectionString="Data Source=My-Pc;Initial Catalog=Db1;User ID=StackOverflow;Password=123456"></add>
</connectionStrings>-->
Public void MyMethod(string regionId)
{
string str = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString; // null
string str = System.Configuration.ConfigurationManager.AppSettings["connectionstring"];// null
}
Note : MyMethod is running on threadpool:
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod), new object[] { regionId });
Did you try....
using System.Web.Configuration;
WebConfigurationManager.AppSettings["connectionstring"]
My connection String in web.config file:
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-EFcodefirst-20131213155231;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-EFcodefirst-20131213155231.mdf" />
</connectionStrings>
My context File
namespace EFcodefirst.Models
{
public class SampleContext: DbContext
{
public DbSet<EFcodefirst.Models.Customer> Customers { get; set; }
}
}
My controller
SampleContext dbContext = new SampleContext();
var customerList = dbContext.Customers.ToList();
return View(customerList);
Here is the 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.
Please help me to solve this problem
Looks like you are trying to create database in same file which is used by ASP.NET membership and you don't have SQLEXPRESS installed on your machine (otherwise Entity Framework would create database with YourNamespace.SampleContext name in your SQLEXPRESS database). So, just add another connection string which will point to another database file:
<connectionStrings>
<add name="SampleContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Sample;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Sample.mdf" />
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-EFcodefirst-20131213155231;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-EFcodefirst-20131213155231.mdf" />
</connectionStrings>
NOTE: If you don't want connection string to have same name as your context class, you can provide connection string name to constructor of base context class:
public class SampleContext: DbContext
{
public SampleContext()
: base("AnotherConnectionStringName")
{
}
public DbSet<EFcodefirst.Models.Customer> Customers { get; set; }
}
Modify your dbcontext file :
public class SampleContext: DbContext
{
public SampleContext()
: base("DefaultConnection")
{
}
public DbSet<EFcodefirst.Models.Customer> Customers { get; set; }
}
i am trying to get connection string from method resulting error object reference not set to the instance object my method is,
this method return connection,
namespace InspectionServices.Services
{
public class ConfigManager
{
public static string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["***"].ConnectionString;
}
}
}
and here i am getting connection by calling method mentioned above,
string connectionString = InspectionServices.Services.ConfigManager.GetConnectionString();
hopes for your suggestion
Thanks in Advance
EDITED:
Appconfig,
<connectionStrings>
<add name="Inspection" connectionString="Data Source=***;Database=***;Integrated Security=SSPI; Persist Security Info=false; Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>
</connectionStrings>
You mush have web.config entries like this
<connectionStrings>
<add name="name" connectionString="***" />
</connectionStrings>
Only then you can use it in your class.
Some useful links
C# Configuration Manager . ConnectionStrings
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
namespace InspectionServices.Services
{
public class ConfigManager
{
public static string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["Inspection"].ConnectionString;
}
}
}
I am making an attempt to establish a connection to a MySQL server using BLToolkit, and have installed MySql.Data (6.5.4), BLToolkit (4.1.12) and BLToolkit.MySql (4.1.12) via NuGet. I can make a connection to a MSSQL server in a single line, but have had trouble with MySQL and ended up with the following configuration file ...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<BLToolkit>
<dataProviders>
<add type="BLToolkit.Data.DataProvider.MySqlDataProvider" />
</dataProviders>
</BLToolkit>
<configSections>
<section name="BLToolkit" type="BLToolkit.Configuration.BLToolkitSection, BLToolkit.4" />
</configSections>
<connectionStrings>
<add name="Test"
connectionString="Data Source=localhost;Port=3306;Database=bltest;User ID=root;Password=root;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
I have extended the DbManager class to implement a reference to the tables, and passed the name of the connection string into the base class. This is how I implemented this behaviour, which should be telling BLToolkit to load the connectionString from the configuration file ...
class BlDb : DbManager {
public BlDb()
: base("Test") {
return;
}
public Table<Car> Car { get { return GetTable<Car>(); } }
public Table<Make> Make { get { return GetTable<Make>(); } }
}
An exception, however, is thrown. The exception is "The type initializer for 'BLToolkit.Data.DbManager' threw an exception." with the inner exception being "Configuration system failed to initialize". How should I proceed? Please note that a similar question does exist on SO, Getting BLToolkit to work with MySQL, which might be a helpful reference for you but doesn't make any sense whatsoever to me. Is installing both NuGet packages not enough?
Firts you need to add the reference to the BLToolkit.Data.DataProvider.MySql.4.dll to your project. Then modify your extended DbManager class to look as the following
class BlDb : DbManager
{
public BlDb()
: base( new BLToolkit.Data.DataProvider.MySqlDataProvider(), "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword" )
{
}
public Table<Car> Car { get { return GetTable<Car>(); } }
public Table<Make> Make { get { return GetTable<Make>(); } }
}
you can replace the hard-coded connection string and return it from your app.config file like ConfigurationManager.ConnectionStrings["Test"].ConnectionString