MVC 3 Scaffolding Template creation error - asp.net-mvc-3

I'm trying to generate a Controller usind the Scaffolding Template "Controller with read/write actions and views, using Entity Framework".
I created an ADO.net Data Entity Model and connected it to an MSSQL 2008 DB (hosted by GoDaddy) and I am able to select the correct Model class and Data context class, but when I click "Add", I get the following error:
Unable to retrieve metadata for 'Project.Models.ModelClass'. The entry 'ProjectDataContext' has already been added. (C:\Users...\Temp\tmpD167.tmp line 13)
The file "tmpD167.tmp" is a copy of my web.config
Line 13 is a connection string that looks like this (post sanitation):
<add name="ProjectDataContext" connectionString="metadata=res://*/Models.Project.csdl|res://*/Models.Project.ssdl|res://*/Models.Project.msl;provider=System.Data.SqlClient;provider connection string="data source=project.db.12345678.hostedresource.com;initial catalog=projectdb;persist security info=True;user id=projectdbuser;password=Password00##;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I have 2 other connection strings (one for ApplicationServices and the other for the project data) that are on lines 11 and 12.
Is my connection string formatted correctly?

can you remove your .edmx file and the reference to it in the web config and thenrecreate it?

Related

Entity Framework - Connection string - Keyword not supported 'Data Source'

I know this is a common issue faced by beginners in EF and there have been multiple questions and answers on the same here, but still I have not been able to still resolve the issue.
I am using EF 4.1 and MVC 3 and have generated the EF model from the DB in a separate library. I have copied the connection string from the app.config in the supporting library to the web.config of my application. I am also instantiating the object context by passing the web.config connection string.
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MasterDataContainer"].ConnectionString;
context = new MasterDataContainer(connectionString);
The connection string in web.config is as below
<connectionStrings>
<add name="MasterDataContainer" connectionString="metadata=res://*/MasterData.csdl|res://*/MasterData.ssdl|res://*/MasterData.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=MasterData;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I am getting the error "Keyword not supported : data source".
Any help on this is appreciated.
You don't need the EntityConnectionStringBuilder since you already have an EF connection string. i.e. just
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MasterDataContainer"].ConnectionString;
context = new MasterDataContainer(connectionString);
EntityConnectionStringBuilder can be used to build up an EF connection from e.g. a vanilla .NET one.
Edit
It looks like you've encountered this problem here. The workaround is to escape the load the connection string and then replace the " with "'"
What might be easier altogether is to use the name=ConnStringName overload of ObjectContext / DbContext. In your case, this would be
context = new MasterDataContainer("name=MasterDataContainer");
Note also if you view the source of the generated Context (probably MasterDataContainer.Context.cs) that the default constructor should have the EntityContainerName property hardcoded into it, so you might not need to provide a connectionstring at all to the constructor, provided that you keep the same EntityContainerName.
http://msdn.microsoft.com/en-us/library/bb739017.aspx
easiest way to solve this issue is rewrite your EF conn string (replacing the amp; and quot;):
from ORIGINAL
<add name="Entities" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=<SERVER\INSTANCE>;initial catalog=<BDD>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"" providerName="System.Data.EntityClient" />
to FIXED
<add name="Entities" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string='metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient';provider connection string='data source=<SERVER\INSTANCE>;initial catalog=<BDD>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;'" providerName="System.Data.EntityClient" />
Just change then Entities name and the tags SERVER\INSTANCE and BDD with your values and that's it.
I hope this saves you a few days of research. Enjoy!

Exception thrown when creating sqlite .net entity data model

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.

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