.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Using Multiple Entity Framework Entities in MVC2

Posted By:      Posted Date: September 29, 2010    Points: 0   Category :ASP.Net

I need help getting data from multiple Entities models into an MVC controller.  Most of the examples I have seen for using EF in an MVC2 app only use a single entitiy.

I have just started using MVC2 in C# using the Entity Framework models (CIOps.model) created from an SQL database.   I can create the controller and views using single tables of the model in MVC2, but I just cannot get my head around how to get data from multiple entity tables into the controller (similar to joins in T-SQL).  I have included an example below of the controller code that works with a single entity table, tbl_tours (tbl_tour in DB).

Could someone please illustrate how this code would be changed to include additional columns from FK tables in addition to tbl_tours columns.  E.g. the clients name from the tbl_clients, the coordinators name from the tbl_employees, and costs from tbl_costs entities?  Is it possible to do this directly using the EF model entities/classes that are already created and not use LINQ, POCO, Repositories, etc.? The FK relationships are already in the EF models. 

I have included the whole controller code, but I just need a few examples of how to join the multiple entities, not rewrite every CRUD function function in the controller.  I think this will get me over the hump in using EF in an MVC2 App.  Also i

View Complete Post

More Related Resource Links

Multiple database support with Entity Framework

One of the features introduced in Entity Framework is being database independent. Which mean each database provider can support Entity Framework by implementing its provider.

This feature allows you build applications independent from the underplaying database provider. In this post I'm going to show how to build an application that support multiple databases using same conceptual model made by Entity Framework.

ASP.NET 4.0 Dynamic Data and Many to Many Entity Framework Entities

I did not play much with Dynamic Data controls in VS2008 and just made a cool discovery in VS2010 Beta 2. This may not even be new, but as I'm sitting 30,000+ feet over the Atlantic ocean, I don't have access to VS2008 at the moment to check.

Entity Framework POCO and WCF Data Service - Lazy-loading (expand?) related entities?

I've followed ScottGu article on creating an Entity Framework POCO classes for NerdDinners (right now there are only 2 classes, Dinner and RSVP. I then created a WCF Data Service class, which points to my EF class (NerdDinner), everything is working fine, except I can't lazy-load or expand related entities. For example, I want to get all load all the Dinners and their related RSVPs, something like this throw an error:var result = (from d in db.Dinners.Expand("RSVPs")                      select d);The error is a null reference error:at System.Data.Services.Serializers.SyndicationSerializer.WriteObjectProperties(IExpandedResult expanded, Object customObject, ResourceType resourceType, Uri absoluteUri, String relativeUri, SyndicationItem item, DictionaryContent content, EpmSourcePathSegment currentSourceRoot) at System.Data.Services.Serializers.SyndicationSerializer.WriteEntryElement(IExpandedResult expanded, Object element, ResourceType expectedType, Uri absoluteUri, String relativeUri, SyndicationItem target) ... ... ...Any idea why or what I need to do to load related contents?Thank you very much.

How to display image from database using MVC2 & Entity framework.



I am very new to MVC and entity I have being searching all over the internet on how to do this. Please help.

I need to do a site similar to http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx but don't know how to display or retrieve the images as in the above website.

Thank you so much!

multiple insert rows using Entity Framework


Dear All,


i'm trying to use Entity Framework to insert into SQL SERVER 2005, using Visual studio 2008.

the following codes are a Button onclick event,

I want to insert into DB multiple rows at a time

protected void Button1_Click(object sender, EventArgs e) 
	MyTable NewRow = new MyTable(); 
	NewRow.columnName = "5"; //columnName is a primary key
	NewRow.columnName = "6"; 


however this does not work,

it seems we can only insert into DB only one row at a time (??)

if i write :

MyTable NewRow1 = new MyTable();

NewRow1.columnName = "5";


MyTable NewRow2 = new MyTable();

NewRow2.columnName = "6";


MyTable NewRow3 = new MyTable();

NewRow3.columnName = "7";


MyTable NewRow10 = new MyTable();

it will success.

but what if i have more rows, like 20~30 rows? i have to seperately write 20~30

MyTable NewRow20 = new MyTable();  ........... MyTable NewRow30 = new MyTable();


?? sorry i'm a beginner now, please anybody can help? better idea?<

how can I get the same nerddinner.Entities as mvc sample nerddinner using ADO.NET Entity Framework


DbClick the code behind file of NerdDiner.edmx,It reads "This code was generated from a template." on the top comment lines.

I generate my own .edmx  file automatically with VS2010, just found that the .designer.cs file didnt contains Objectset,Methods as nerddinner does

IDK which template hanselman used to generate this .edmx files,dont tell me he wrote it all by himself!

Shoud .edmx and its code behind file be generated automaticlly without hard-coding?

WCF - Entity Framework - POCO - Caching with multiple services



We have an architecture where we use Entity Framework with POCO, and the code-first approach (no edmx file). In order to explain the problem I have to explain a little bit about our architecture:

We have one layer which contains Routing Services (for load balancing and security), and a layer of Data Access Services. All services are hosted in IIS.

What we would like is to have caching of our (Entity Framework) Context, so we made the context static, and thought it would be shared between the different services. Well, it's not... 

The scenario (where we noticed the problem) was that we did some changes to an transaction through the Transaction services. We then did a search through the Search services, and found that the data wasn't updated. Well, actually the data are updated, and saved to the DB, it's just that the "Search Context" is out of sync. When retrieving the data from the Transaction service, we got the correct data, as expected.

So, after hours of googling I still haven't found a solution to the problem, and I'm hoping to get some response here.

Creating a Generic Entity Framework 4.0 Repository

With .NET 4.0 right around the corner, I thought it would be cool to download Visual Studio 2010 beta 2 and start playing around with the next release of Entity Framework.

The initial release of Entity Framework came with a great deal of criticism. To make matters worse, there was a large uproar when it was rumored that Microsoft would be abandoning LINQ to SQL, in favor of Entity Framework. This was because, at the time, many developers felt Entity Framework was an inferior technology to LINQ to SQL. To set things right, Microsoft proactively moved forward with improving Entity Framework, in time for the 4.0 release of the .NET Framework. This is good news because my initial impressions, so far, have been nothing but positive.

Entity Framework 4 Tips for WinForms Development

I've been recently working on a smart client (WinForms) application backed by Entitiy Framework 4 (EF4) on Visual Studio 2010. In this blog post I will try to give you some tips regarding some limitations and points to be careful about EF4.

Data accessors or Entity framework


The entlib 5 sugest me to use with data accessors for mapping data from database to my data objects (ORM).

But in .NET 3.5 and especially in .NET 4, I can use in entity framework as ORM methodology for data objects.

What data accessors uses advantages over the entity framework?

What differences that should be affect on my using decisions?

Data Points: Deny Table Access to the Entity Framework Without Causing a Mutiny


Julie Lerman shows database administrators how to limit access to databases from the Entity Framework by allowing it to work only with views and stored procedures instead of tables-without impacting application code or alienating developers.

Julie Lerman

MSDN Magazine August 2010

VS 2010: Entity Framework 4.0 and WCF Data Services 4.0 in Visual Studio 2010


Elisa Flasko takes you through a sample weblog application to demonstrate how new improvements in Entity Framework and WCF Data Services work together to simplify the way data is modeled, consumed and produced in Visual Studio 2010.

Elisa Flasko

MSDN Magazine April 2010

N-Tier Apps and the Entity Framework: Building N-Tier Apps with EF4


This article is the third in a series about n-tier programming with the Entity Framework, specifically about building custom Web services with the Entity Framework and WCF. This article looks at features coming in the second release of the Entity Framework (EF4) and how you use them to implement the Self-Tracking Entities and Data Transfer Objects (DTOs) n-tier patterns.

Daniel Simmons

MSDN Magazine November 2009

Entity Framework: N-Tier Application Patterns


This article examines n-tier patterns for success and some of the key APIs and issues specific to the Entity Framework. It also provides a sneak peak at features coming in the Microsoft .NET Framework 4 that should make n-tier development significantly easier.

Daniel Simmons

MSDN Magazine August 2009

Entity Framework: Anti-Patterns To Avoid In N-Tier Applications


Danny Simmons explores some anti-patterns you should look out for when building n-tier applications with the Entity Framework.

Daniel Simmons

MSDN Magazine June 2009

SOA Data Access: Flexible Data Access With LINQ To SQL And The Entity Framework


Building a data access layer using LINQ to SQL and the ADO.NET Entity Framework allows you to decouple your application from the persistence technology you're using.

Anthony Sneed

MSDN Magazine December 2008

ADO.NET: Achieve Flexible Data Modeling with the Entity Framework


Here the author answers questions regarding the Entity Framework and provides an understanding of how and why it was developed.

Elisa Flasko

MSDN Magazine July 2008

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend