<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET MVC</title><link>http://forums.asp.net/1146.aspx</link><description>Discussions regarding Model-View-Controller (MVC) support in ASP.NET.  &lt;a href="http://forums.asp.net/1215.aspx"&gt;T4MVC subforum&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274666.aspx</link><pubDate>Sat, 04 Jul 2009 12:57:08 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274666</guid><dc:creator>paul.vencill</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274666.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274666</wfw:commentRss><description>&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="/Themes/fan/images/icon-quote.gif"&gt; &lt;strong&gt;gerrylowry:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;
&lt;p&gt;I do not think it is a problem.&amp;nbsp; In a complex application, one could be connected to multiple databases.&lt;/p&gt;
&lt;p&gt;I would hope that with two connections to the &lt;em&gt;same&lt;/em&gt; database, there might be some efficiency that one gets automatically.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not a database expert, regardless, this seems normal to me.&amp;nbsp; In our application, we are abstracting to a logical model of our data.&amp;nbsp; The physical model should be more or less irrelevant.&amp;nbsp; We should be able to restructure the physical model without changing the logical model AFAIK.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Gerry&lt;/p&gt;
&lt;p&gt;&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I wanted to comment on this.&amp;nbsp; I&amp;#39;m not a database expert, either, but I&amp;#39;ve been on all sides of them through my career (install, dba, develop, app developer, etc).&amp;nbsp; I wanted to drill down a bit on the pro&amp;#39;s and cons of the scenarios here.&lt;/p&gt;
&lt;p&gt;Multiple Db&amp;#39;s.&amp;nbsp; Using multiple connections in and of itself isn&amp;#39;t a problem; doing so synchronously and in series is probably a reliability and performance issue, especially if one considers that you may be taking a network hit to talk to them.&amp;nbsp; Consider that in the above code, with two DBs, you are forced to wait for the first to respond before calling the second.&amp;nbsp; If the entities are not related, there&amp;#39;s no good reason for that and the whole thing can (and should) be done using asynchronous calls (though that ups the complexity of the solution).&amp;nbsp; If they are related, you can still use asynch methods, you just have to use callbacks to pass the keys needed between&amp;nbsp;them.&amp;nbsp; In both cases, you have to consider&amp;nbsp;how&amp;nbsp;to notify your user if things went wrong, etc.&amp;nbsp; A potentially more elegant solution&amp;nbsp;would be to use a message queue of some sort (or bus that guarantees&amp;nbsp;reliable messaging) so that you can respond&amp;nbsp;to the user that &amp;quot;all is well&amp;quot; as soon as the&amp;nbsp;objects hit the queue, since the&amp;nbsp; queue&amp;#39;s job&amp;nbsp;is to make sure that the transaction happens successfully.&lt;/p&gt;
&lt;p&gt;Multiple connections to the same DB.&amp;nbsp; Less of an issue.&amp;nbsp;&amp;nbsp;Yes, ADO.Net&amp;nbsp;(and by extension L2S and L2E) does&amp;nbsp;pool connections, so that you can take less of a hit for opening new ones, but you do still *use* two connections out of that pool, and that&amp;#39;s still a finite resource.&amp;nbsp; In a high traffic site, you could still &amp;nbsp;run into issues.&amp;nbsp; While you can change the # of pooled connections, that changes the physical resource consumption profile of your app, and why hasten the rate at which your app uses resources needlessly?&amp;nbsp; &lt;/p&gt;
&lt;p&gt;That said, one of the things that most of the modern ORMs do pretty well (L2S, L2E, NHibernate, LLBLGENPro) is minimizing footprint on the connection pool.&amp;nbsp; They grab a connection at teh very last second, and release it as soon as possible.&amp;nbsp; So the danger is probably minimal for using multiple &amp;quot;contexts&amp;quot; (using the generic term) at the same time.&amp;nbsp; So, multiple connections at the same time == bad.&amp;nbsp; Multiple contexts, probably less so.&lt;/p&gt;
&lt;p&gt;Finally, I would agree that in a perfect world the logical model would be completely ignorant of the persistence means, and the physical model could be changed willy-nilly.&amp;nbsp; I have found that from a practical standpoint, that&amp;#39;s not always the case.&amp;nbsp; Some ORM tools are better than others at allowing that (e.g. NHhibernate is better at that than L2S or L2E), but there are always tradeoffs.&amp;nbsp; If you don&amp;#39;t want to expose your properties as virtual, and you&amp;#39;re using NHibernate, then you either have to tell it to bypass your properties and go straight for the fields (which you may not want to do) or else you have to accept not lazyloading them.&amp;nbsp; Which may or may not be a good choice for you, performannce-wise.&amp;nbsp; L2S, you have to accep ttha tyou&amp;#39;ll only be using SQL Server.&amp;nbsp; The list goes on.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;/p&gt;</description></item><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274641.aspx</link><pubDate>Sat, 04 Jul 2009 12:36:11 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274641</guid><dc:creator>paul.vencill</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274641.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274641</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;Sorry, missed the question about model binding.&amp;nbsp; That&amp;#39;s fairly straightforward.&amp;nbsp; The easiest case is to create a viewmodel that wraps both, like this:&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public class MyViewModel
{
    public FirstEntity Model1 {get; set;}
    public SecondEntity Model2 {get;set;}
}&lt;/pre&gt;
&lt;p&gt;Your view would then inherit from ViewPage&amp;lt;MyViewModel&amp;gt; for example, adn you would write your form fields using syntax like this:&lt;/p&gt;
&lt;p&gt;&amp;lt;%= Html.TextBox(&amp;quot;Model1.Name&amp;quot;) %&amp;gt;&lt;br /&gt;&amp;lt;%= Html.TextBox(&amp;quot;Model2.Name&amp;quot;) %&amp;gt;&lt;/p&gt;
&lt;p&gt;The method that receives the post should expect a parameter of the type of the viewmodel as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Hope that helps,&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;</description></item><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274635.aspx</link><pubDate>Sat, 04 Jul 2009 12:30:20 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274635</guid><dc:creator>paul.vencill</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274635.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274635</wfw:commentRss><description>&lt;p&gt;Your context instance should provide change tracking for you.&amp;nbsp; I gather from the original poster that we&amp;#39;re talking about ONE context, but TWO models in that context. Maybe I&amp;#39;m mistaken.&lt;/p&gt;
&lt;p&gt;IN the case that I&amp;#39;m correct, then you only need to instantiate one context and save changes once after applying the changes to both entities; it&amp;#39;ll navigate the relationships and such to make sure keys line up.&lt;/p&gt;
&lt;p&gt;Also in that case, you&amp;#39;re only tying up one connection.&lt;/p&gt;
&lt;p&gt;IF you meant to be hitting two *different* datasources, then Gerry&amp;#39;s correct on all counts.&lt;/p&gt;</description></item><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274361.aspx</link><pubDate>Sat, 04 Jul 2009 07:04:16 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274361</guid><dc:creator>gerrylowry</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274361.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274361</wfw:commentRss><description>&lt;p&gt;I do not think it is a problem.&amp;nbsp; In a complex application, one could be connected to multiple databases.&lt;/p&gt;
&lt;p&gt;I would hope that with two connections to the &lt;em&gt;same&lt;/em&gt; database, there might be some efficiency that one gets automatically.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not a database expert, regardless, this seems normal to me.&amp;nbsp; In our application, we are abstracting to a logical model of our data.&amp;nbsp; The physical model should be more or less irrelevant.&amp;nbsp; We should be able to restructure the physical model without changing the logical model AFAIK.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Gerry&lt;/p&gt;</description></item><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274357.aspx</link><pubDate>Sat, 04 Jul 2009 06:58:45 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274357</guid><dc:creator>LeMerovingian</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274357.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274357</wfw:commentRss><description>&lt;p&gt;Thanks for your reply. I should have mentioned that I am not using Linq2Sql.&lt;/p&gt;&lt;p&gt;I am looking for a solution of how to insert more than one entity using the Model Binding approach?&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274312.aspx</link><pubDate>Sat, 04 Jul 2009 06:11:15 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274312</guid><dc:creator>zielony</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274312.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274312</wfw:commentRss><description>&lt;p&gt;I have interesting question. In that case we use two Models - in each model we have:&lt;/p&gt;&lt;p&gt;private databaseDataContext db = new databaseDataContext();&lt;/p&gt;&lt;p&gt;it means two connections to database !! Is it ok in ASP.NET MVC ??&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3274130.aspx</link><pubDate>Sat, 04 Jul 2009 01:07:27 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3274130</guid><dc:creator>gerrylowry</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3274130.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3274130</wfw:commentRss><description>&lt;p&gt;I&amp;#39;d try something like &lt;/p&gt;
&lt;p&gt;private Your&lt;strong&gt;First&lt;/strong&gt;TableDBEntities _db&lt;strong&gt;First&lt;/strong&gt; = new Your&lt;strong&gt;First&lt;/strong&gt;TableDBEntities();&lt;br /&gt;private Your&lt;strong&gt;Second&lt;/strong&gt;TableDBEntities _db&lt;strong&gt;Second&lt;/strong&gt; = new Your&lt;strong&gt;Second&lt;/strong&gt;TableDBEntities();&lt;/p&gt;
&lt;p&gt;[AcceptVerbs(HttpVerbs.Post)]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public ActionResult Edit( ...&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var&amp;nbsp;secondTableStuff = (from m in _db.SecondTableSet where m.Id == firstTableForeignKey.Id select m).First();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _db.ApplyPropertyChanges(firstTable.EntityKey.EntitySetName, firstTable);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_db.SaveChanges();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _db.ApplyPropertyChanges(secondTable.EntityKey.EntitySetName, secondTable);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_db.SaveChanges();&lt;/p&gt;
&lt;p&gt;People more expert than me can help you with the correct syntax.&amp;nbsp; Syntax is always torturing me.&lt;/p&gt;
&lt;p&gt;I would not be surprised if there are more elegant solutions.&lt;/p&gt;
&lt;p&gt;TIMTOWTDI =. there is more than one way to do it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Gerry (Lowry)&lt;/p&gt;</description></item><item><title>MVC - Insert data into multiple entities from same page</title><link>http://forums.asp.net/thread/3273679.aspx</link><pubDate>Fri, 03 Jul 2009 14:28:37 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273679</guid><dc:creator>LeMerovingian</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273679.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3273679</wfw:commentRss><description>&lt;p&gt;I have a single page which collects related data into a series of input fields.&lt;br /&gt;&lt;br /&gt;When the user clicks submit I want to insert the data into two different database tables in one go. ie I want to get the primary key from the first insert, and then use it for the second insert operation.&lt;br /&gt;&lt;br /&gt;I am hoping to do this all in one go, but I am not sure of the best way to do this with the Models/Entities in MVC.&lt;/p&gt;</description></item></channel></rss>