While working on an ASP.NET MVC application I encountered the following error. It clearly says that some connection string is missing in the configuration file.
Well I have refactored the application following the onion architecture and there is nothing related to database exist in the MVC project. Project structure looks like follows:
The Entity Framework is part of the infrastructure project and in its App.Config file, there is an entry of connection string as follows:
Even though connection is present in the project which is dealing with the database, error is thrown. The reason behind this is, the MVC project is a startup project and the infrastructure project is not a startup project. We need to have connection string in the configuration file of the startup project. In this case startup project is MVC project, so to solve this error we need to copy IndiaMP connection string to Web.config of MVC project. After doing that you should not get the above error.
Solution: Copy connection string to configuration file of startup project.
If you are using StructreMap to resolve the dependencies in the ASP.NET MVC 5 application then Steve Smith’s blog post Resolving Dependencies in ASP.NET MVC 5 with StructureMap can be a great help for you. I have followed Steve’s blog to use the StructureMap. I am summarizing the steps as follows:
Using the Nuget Package Manager add package of StructureMap.mvc5 in the project
Refactor controller to use the constructor injection.
If you are following the default convention means IMovieRepository should be resolved to MovieRepository then you should not care about any other files. However you can override default behavior in the DefaultRegistry class which is inside DependencyResolution folder. As you notice below that StructureMap is scanning with default convention.
An Important Note
Example shown in Steve blog is using Repository from same MVC project. What if Repository is not part of the same MVC project, in this case you need to explicitly specify in the StructureMap DefaultRegistry class that scan the assembly containing the Repository. This can be specified as follows:
As you see that we are explicitly specifying that the scan assembly which contains MovieRepository. In this case MovieRepository is part of another project MvcMovie.Infrastructure.MovieRepository. Other important class is StructuremapMvc which is inside App_Start folder.
This is all you need to configure to resolve dependency using StructureMap in the ASP.NET MVC application.
Read full post on Falafel blog
I have found that many entry level developers get confused with various kinds of Views in ASP.NET MVC Applications. In this post I am trying to clarify various types of Views.
Type of ASP.NET MVC View depends on the way data being passed to it from Controller. There are three ways data can be passed to Views from Controller. They are as follows,
- Using ViewBag or ViewData
- Using object of the model which is strongly typed
- Using any dynamic type value
On basis of data transfer mechanism ASP.NET MVC views are categorized as two types,
- Dynamic view
- Strongly typed view
Read full post on Falafel blog
Have you ever thought of viewing generated SQL query of LINQ you write? You may need the generated query for the debugging and the logging purposes. In this post let us see how to print the generated SQL query on the console.
Let us consider you have written LINQ as follows:
DataClasses1DataContext context = new DataClasses1DataContext();
IEnumerable<Order> result = context.Orders;
var product = result.Where(x => x.OrderID == 10248);
context.Log = Console.Out;
foreach(var r in product)
You can print the generated SQL by putting one line of code just before data gets loaded. In this scenario put following line of code anywhere but before the foreach statement.
You will get the generated SQL query in console as follows:
You can set or get log of the DataContext in a TextWriter. If required you can save them on a file system etc.