publishing Db after creating using EF Code first - asp.net-mvc-3

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.

Related

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:

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!

MVC3 Forms Authentication - going online

I'm using the AuthorizeAttribute and the default forms authentication that comes with the "Internet Application" preset.
Everything is working as it should, but I have some questions about deployment.
I've got this in the web.config:
<connectionStrings>
<add name="AlltForMusikContext" connectionString="Data Source=|DataDirectory|AlltForMusik.sdf" providerName="System.Data.SqlServerCe.4.0"/>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
I tried to get it to use the database in "AlltForMusikContext" but with no success. When I deploy the site, I will only have one (1) SQL-database.
How does this work really? Is the "aspnetdb.mdf" some special db or do I need to get it to use the db in "AlltForMusikContext"?
I couldn't find any relevant info about this, so I would really appreciate your input!
Best regards
Mikael
you are right, your application is having default "aspnetdb.mdf" in you application under "App_Data" folder.
putting AspNetDb.mdf file under application folder is not a good practice rather than you can create a separate database with name and use that. putting your membership database along with your application database does not make good sense.

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

SQL Server 2008 connection string for entity framework 4.1 code first

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>

Resources