System.Data.Entity.Core.MetadataException: 'Unable to load the specified metadata resource.' - asp.net-web-api

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

Related

Database Connection String error

Currently I am running MVC project using Oracle as my database.
When my solution runs, it throws the exception:
'entitycommandexecutionexception'.
I thought my webconfig file has some issues, so I looked in my webconfig. When I tried to save it, my file was saved with the an error message
"The 'data source' keyword is not supported".
My webconfig connectionstring is as follow:
<connectionStrings>
<add name="bob" connectionString="metadata=res://*/Entities.bob.csdl|res://*/Entities.bob.ssdl|res://*/Entities.bob.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=localhost/XE;PASSWORD=bobsamuel;PERSIST SECURITY INFO=True;USER ID=bobsamuel"" providerName="System.Data.EntityClient" />
</connectionStrings>
Your connection string is wrong. Pay attention on "providerName" part. Your's currently set to "System.Data.EntityClient" but it should be "Oracle.ManagedDataAccess.Client" - if you are using official Oracle driver.

how to deploy asp.net mvc 3 where LINQ to SQL Classes is used?

i'm new in LINQ.I have used LINQ to SQL class in my asp.net mvc 3.now when it comes to deployment in iis how can i manage connection ?i'm stuck here.help plz ,thankx in advance
Are you managing the connection via Web.config?
<connectionStrings>
<remove name="MyConnectionString"/>
<add name="MyConnectionString" connectionString="XXXXXXXXXX"
providerName="System.Data.SqlClient" />
</connectionStrings>
You should verify that the connectionStrings is pointing to the right SQL server
edit-1
Just to be sure we are on the same page on "managing the connection via Web.config" I mean via the partial class:
public partial class MyDataContext{
public MyDataContext()
: base(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString,mappingSource)
{
}
}
And of course you need to change also:

Keyword not supported: 'initial catalog'. MVC, ADO.NET Entity Framework

I have a MVC WebAPI project and a ADO.NET Entity Framework project in one solution. I am connecting and working with the database from the ADO.NET Entity Framework project, but when I add the ado.net project to the MVC WebAPI project so that I can store data in the database the same connection string is not working that works in the ADO.NET project.
I've copied the connection string from the ado.net project and pasted it into the web.config file of the mvc project and I am getting the initial catalog keyword not supported.
Below is the connection string that connects perfectly from the ADO.NET project but does not work in the MVC project.
<add name="StorefrontSystemEntities1"
connectionString="metadata=res://*/StorefrontSystemDB.csdl|res://*/StorefrontSystemDB.ssdl|res://*/StorefrontSystemDB.msl;provider=System.Data.SqlClient;provider connection string=data source=localhost;initial catalog=StorefrontSystem;persist security info=True;user id=xxxxx;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.EntityClient" />
What am I missing here?
I am not sure exactly what happened here but it is working now.. I noticed that for provider connection string the " " are " &quot.
<add name="StorefrontSystemEntities1"
connectionString="metadata=res://*/StorefrontSystemDB.csdl|res://*/StorefrontSystemDB.ssdl|res://*/StorefrontSystemDB.msl;
provider=System.Data.SqlClient;
provider connection string="data source=localhost;initial catalog=StorefrontSystem;persist security info=True;
user id=XXX;password=XXXXX
MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
I have no idea what happened here.

connecting to SQL Server 2008 using MVC 3 ASP.net

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")
{
}
}

publishing Db after creating using EF Code first

I have created a project in MVC 3 using code first and nugget ,
And I would like to clear a few thing before publishing to my shared hosting:
In my project I have a class name: IchudShulContext (IchudShul is my project name)
In my sql server express it has created a DB name: IchudShul.Models.IchudShulContext.dbo
Does it make a different what name I give my shared hosting DB ?
Or should it match one of the following : IchudShul / IchudShulContext
My local connectionStrings look like this :
connectionString="Data Source=MyPc-MAINPC\SQLEXPRESS;Initial Catalog=IchudShul.Models.IchudShulContext;Integrated Security=True"
providerName="System.Data.SqlClient" />
Thanks
Based on Code-First convention, Your ConnectionString name in your web.config should have the same name as your context. Database name is not important. in your scenario, in your web.config:
<connectionStrings>
<add name="IchudShulContext" providerName="System.Data.SqlClient"
connectionString="Data Source=MyPc-MAINPC\SQLEXPRESS;Initial Catalog=WHATEVER_YOUR_DB_NAME_IS;Integrated Security=True" />
</connectionStrings>
If you want to use conventions, make sure the name attribute is: IchudShulContext. That's all. Fill in WHATEVER_YOUR_DB_NAME_IS with whatever you db name is.
Your shared hosting DB can be named anything.
Your ConnectionString should be updated needs to be updated to point to your database. What that is, you will need to know from your shared hosting provider.
you can name you DB anything, as long as it is valid with respect to your DBMS. the only this that should be matched with your datacontext name is connection name in connection strings section of your web.config file.

Resources