I have a webservice that I am using to get information from users. Before installing the service, it works fine. After installing the service, I am getting an exception "Object reference not set to an instance of an object." The WCF is referencing a dll where validating and writing to the database and return the results. The dll library has an app config where I added the connection string
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear/>
<add name="reqInformation" connectionString="Data Source=databasesourceInitial Catalog=databasename;Persist Security Info=True;User ID=username;password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
I have this class that is getting the connection string
public abstract class BASEClass
{
public static string GetConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["reqInformation"].ConnectionString;
}
}
}
I added the same connection string to the wcf config file.
In addition, I added a test project to test the service before installing it, it worked fine. I installed the WCF Service and ran the test project again, and it failed. The exception was Object reference not set to an instance of an object.
What am I doing wrong here?
The app config was added to the service Library; however, the service library was not the executing assembly. As a result, it was giving that exception. I moved the connection string int he executing assembly, recompile the code, re-install the service, and test it. All things are working as expected.
Related
I have WebApi solution which has WebApI project and DataModel project(Class Library).
DataModel project has WebApiDbModel.edmx.
Below is the connectionstring :
<connectionStrings>
<add name="WebApiDbEntities" connectionString="metadata=res://*/WebApiDbModel.csdl|res://*/WebApiDbModel.ssdl|res://*/WebApiDbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MachineName\SQLEXPRESS;initial catalog=DbName;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I have copied my connectionstring to Web.config of WebApi project.
When I run the project locally and make call using postman, I am getting below exception in DataModel project code:
System.Data.Entity.Core.MetadataException: 'Unable to load the specified metadata resource.
I tried changing connectionstring portion as below:
connectionString="metadata=res://*/ but this don't work.
Using VS 2017 and EF version is 6.1.3
I have 3 class projects in my solutions.
1. MVC4 project
2. Domain
3. Tests
In the MVC4 project I have added a web.config file with the connection string as
<add name="EfDbContext" connectionString="Data Source=.;Initial Catalog=SportsStore;Integrated Security=true;" providerName="System.Data.SqlClient"/>
I have a class by name EfDbContext in Domain project which inherits DbContext.
When I view the website in browser, I get the server not found error.
I debugged the EfDbContext class and found that the Database.Connection.ConnectionString
is set to \SqlExpress with database as EfDbContext.
Why is that?
You must have your connect string as the FULL namespace to your context when your context class is in a different project
For example:
<add name="YourClassProject.EfDbContext" connectionString="Data Source=.;Initial Catalog=SportsStore;Integrated Security=true;" providerName="System.Data.SqlClient"/>
You should create your DbContext with your connection string name passing to the constructor
public class UnicornsContext : DbContext
{
public UnicornsContext()
: base("EfDbContext")
{
}
}
http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx
Finally, I realized that I added the connection entry in the web.config file created by VS in Views folder.
My bad.
I'm trying to get sqlite working with linq. So far I've:
installed the ADO.net providers from http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/
installed both the 32 and 64 bit sqlite .net from http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
Now I can create an entity data model using an sqlite database as the source. Whenever I try to create a new instance of the data model (in a wpf component that is getting displayed) I get the following exception:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type myprogram.mycomponent' that matches the specified binding constraints threw an exception.' Line number '13' and line position '37'.
The mycomponent constructor is simply:
private void myprogram()
{
InitializeComponent();
pricesEntities pe = new pricesEntities();
}
When I open the design view for the window that displays the component, I get an
ArgumentException was thrown on "mycomponent": Cannot create an instance of "mycomponent".
An Unhandled Exception has occurred
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at myprogram.pricesEntities..ctor() in <path>\prices.Designer.cs:line 34
at myprogram.ItemList.InitializePriceList() in <path>\ItemList.xaml.cs:line 34
at myprogram.ItemList..ctor() in <path>\ItemList.xaml.cs:line 29
My app.config contains the following connection string:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="pricesEntities" connectionString="metadata=res://*/prices.csdl|res://*/prices.ssdl|res://*/prices.msl;provider=System.Data.SQLite;provider connection string='data source="<path>\prices.db"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
I tried copying the app.config file to the directory of the executable, as was suggested here and other places. That did not help. Note that this is a different issue than The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
I have only one project (a wpf application) that I am trying to get to work.
Figured out it was because it was an assembly build for the 2.x runtime, so I added the useLegacyV2RuntimeActivationPolicy element to my app.config and that resolved the issue.
I am new to ASP.net's MVC 3 (using VB) and I am trying to create an application that would connect to an SQL 2008 server database. I have gone through a tutorial (Microsoft ASP.net's Movie DB tutorial) but the tutorial uses SQL compact edition. I am having a hard time connecting. Am I correct in assuming that once I create a model, I should be able to just connect to SQL 2008 by changing the connection string in the Web.config file, found at the root of the application folder? I deleted the sql ce database from the App_Data folder. In Microsoft SQL Server Management Studio I created a new database. I then added this to my Web.config file:
<connectionStrings>
<add name="ConnectionName"
providerName="System.Data.SqlClient"
connectionString="Data Source=DELERIUM-PC;Initial Catalog=iDjItDb;Integrated Security=True" />
</connectionStrings>
The when I run the app, and try to view the controller associated with the model, i get this error:
The model backing the 'iDjItDBContext' context has changed since
the database was created. Either manually delete/update the database,
or call Database.SetInitializer with an IDatabaseInitializer instance.
For example, the DropCreateDatabaseIfModelChanges strategy will
automatically delete and recreate the database, and optionally seed it
with new data.
What must I do to connect and work with a 2008 SQL database?
Thanks
jason
You can remove the IncludeMetadataConvention in your context class if you are positive that your model is compatible with the database.
public class iDjItDBContext : DBContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
}
Otherwise you need to set the initializer in the Application_Start() method of your Global.asax.cs file.
Database.SetInitializer<iDjItDBContext>(
new DropCreateDatabaseIfModelChanges<iDjItDBContext>());
Otherwise you can take the Migrations option where an external tool will generate the change script.
Edit
Change the connection string name to iDjItDBContext so that the name matches with the DbContext name.
<connectionStrings>
<add name="iDjItDBContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=DELERIUM-PC;Initial Catalog=iDjItDb;Integrated Security=True" />
</connectionStrings>
Or create a constructor in your context and pass the name of the connection string.
public class iDjItDBContext : DBContext
{
public iDjItDBContext() : base("ConnectionName")
{
}
}
I need a valid SQL Server 2008 connection string for Entity Framework 4.1 code-first project. I would use it now with MVC 3.
For now it's still very simple, only 1 project, 3 simple model class...
I could only find everything else, like Sql Express, CE connections on the web...
Finding it by name in web.config ("ApplicationServices") is OK, because when I tried to use I got specific errors for that.
The best I could get is:
Unable to load the specified metadata
resource.
I tried to give it like metadata=res://MyWebProject/MyWebProject.csdl| ... also but no success.
So it doesn't create the database for me - even doesn't hit the OnModelCreating(DbModelBuilder modelBuilder) method.
If I try to use an 'old fashioned' connection like the ones I found for SQL Server Express, it misses the metadata.
Thanks for your help in advance.
The idea of "Code-First" is, that you shouldn't have to deal with the .csdl, .ssdl and .msl files mentioned in the connection-string. If not specified elsewhere, the DbContext will lookup the connection-string in the web.config as you described. The constructor of the DbContext class will accept a name-value pair specifiying the name of the connection-string in the web.config. For example:
<connectionStrings>
<add name="ConnectionName"
providerName="System.Data.SqlClient"
connectionString="Server=ServerName;Database=DatabaseName;Integrated Security=True;" />
</connectionStrings>
can be referenced in your context:
class MyContext : DbContext
{
public MyContext() : base("name=ConnectionName") { }
...
}
The sample connection-string I've provided is actually for a SQL Server database. It is important to specify the ProviderName, since the "Code-First" requires it to generate a corresponding .ssdl-File (storage schema).
Besides, you can name your connection string after your DbContext class. In this case you may not mention the name of the connection string
class MyContext : DbContext
{
//public MyContext() : base("name=ConnectionName") { } // not needed
...
}
and the connection string is the folowing
<connectionStrings>
<add name="MyContext"
providerName="System.Data.SqlClient"
connectionString="Server=ServerName;Database=DatabaseName;Integrated Security=True;" />
</connectionStrings>