I am trying to design an .NET MVC application and the application needs to access data from multiple databases. I the only way to do the low level database I/O is by calling MS SQL stored procedures. What I think is the best way to go is to develop the data layer using a combination of WCF as the lowest level calling the actual stored procedures. From there I feel that an Entity Framework layer be setup to be used for the actual MVC design. Can anyone give me a few I ideas to the best way for a successful design?
Since you are going to use entity framework so I think you can go with multiple data access layer.
DALDatabase1
DALDatabase2
Both these database can have their own repositories. This will be the cleaner approach.
You use one layer as well but that will going to be messy in future.
I have used MVC thus far in a traditional EF sense by creating POCO first objects and then adding this to a database context but I have a new problem in that I am trying to recreate a legacy system but using the MVC framework but it uses a connection to multiple different databases and tables.
In the legacy solution they use Enterprise library to create the link to databases but I am not sure that this is the best option available and was wondering what are the options open for dbconext in regards to different database connections at the same time, is this possible?
The question is nothing to do with MVC. Besides, ASP.Net MVC (Presentation Layer) should not even need to know about what kind of ORM or database at Data Access Layer.
Back to original question, it is not worth using Entity Framework, if you have to query two databases at the same time.
I suggest you want to look at other ORM like Dapper.
I have a silverlight application that communicates with the sql server database. The server side is a WCF RIA webservices that use EF 4 for persisting data back to db.
I am considering switching to RavenDB for two reasons
Scalability
Freedom from updating schema on production server.
My questions
How easy it is to switch to RavenDB. Is it as easy as pointing the connection toRavenDB.
Will it create a schema automatically by inspecting the entities.
can i continue to use linq in wcf. Or do i need to replace code in it?
Thanks,
Ankur
No. It will most likely require a model change since a model suited from RavenDB is usually very different from a relational model you're likely to be using now.
There is no schema with RavenDB.
Yes, but you'll probably need to rewrite the queries to match the new document-oriented model that you'll come up with for using RavenDB.
I have been given the task of trying to convert our company's existing ASP.NET Web Forms application into an MVC 4 application (as an R&D starting project). I have a little experience with MVC 2, but my issue is trying to actually get started. We have existing stored procedures that we access via Subsonic as our ORM (so that they can be called in our code). We also have a Data Access Layer that is tied in with Subsonic.
I know this is kind of a vauge question, but is there a known way I can use our existing stored procs and tables with a different ORM (such as NHibernate or something) to start creating this MVC application?
Thanks for any help provided.
There should be no reason that the SP's need to change just because the ORM changes; you may want to to improve some of what you are doing and rewrite some of them, but not because the ORM changed.
I have an asp.net mvc application and i need to connecto to the DB i have saw a tutorial video that connect to DB using wizard by adding DB connection and determine the DB and add a model but i need to know if i can use connection string and query the DB or calling procedures in DB ???
I need any tutorials or step by step article that describe how to connect to DB without wizard and call procedures and query tables.
Thanks in advance and i am a begineer in MVC
I hesitate to respond to this, but EVERY video you have seen is likely using an OR/M to generate the Model and the DAL. The generated DAL will likely encapsulate your calls to the stored procedures that you're asking about.
The thing is -- and here's why you're not getting the answer you're looking for -- each OR/M is going to have a different method of retrieving data from and inserting data into the database. How you retrieve data from the DB using an OR/M is going to be different if you're using Entity Framework, Linq to SQL, SubSonic, NHibernate, or any other OR/M.
So, the question is to you. Are you using a OR/M? If so, which one? If not, then you will use the standard ADO.NET calls to retrieve and store data. This is also reflected in my comment to your original question.
Yes you can (google ado.net for tutorials on ado.net), but it's not the MVC way. The MVC way is to use some sort of ORM (Object-relational mapping) such as NHibernate, Subsonic or Linq for SQL.
how to connect to DB without wizard and call procedures and query tables.
To call a procedure (here it will return no result, just perform some action):
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection .Open();
using(SqlCommand command = connection.CreateCommand(nameofthestoredprocedure))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("#someparameter", somevalue);
maCommande.ExecuteNonQuery();
}
}
You can put your connection string in the config, just like you're used to.
You can call procedures the same way you would from any .Net application too. #svinto's advice about using an ORM is another way of doing things and is well worth looking at too. Many of the good examples for ASP.Net MVC use the ORM techniques.
If you don't want to go down that route, you might benefit from creating a seperate class library project that you reference from your MVC application. Your Class Library project can act as your data access layer (DAL) where you handle db calls, etc...
Your Controllers can then call your DAL and processing methods to populate entities for the Views.
Ahmy, I think the best advice to give would be before you start off developing using the MVC framework is to have a look into the principles behind the MVC pattern and domain driven design.
Specifically , have a look at repository patterns etc.
You can still add connection strings to your web.config in a block and access them as you would have done in a webforms project, after all Asp.net MVC is based on webforms. Its just likely that you wouldn't really want to do this if you're utilising MVC the way it was intended, its all about seperation of concerns.
www.asp.net has some great intro. examples worth watching.
Try downloading NerdDinner, or even better ... ContactManager iteration 1 (the tutorial directly answers your Q). Those should give you a good idea for how to handle database access. I wouldn't suggest looking at something like Oxite or MS StoreFront though, as these are a bit more complex.
To specifically answer your question: the connection string, like in ASP.NET, can go in your web.config or hard-coded in your application.