Code First entity framework connection string - asp.net-mvc-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.

Related

ConnectionString Exception After Installing WebService

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.

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:

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.

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