DotnetSparrow
DotnetSparrow

Reputation: 27996

multiple entities in EF 4.1

I am working on an MVC application which used data from different entities as well showing current date. I want to pass this data from controller to view. Should i create one entity to hold other entities ?

Upvotes: 0

Views: 238

Answers (3)

Turcogj
Turcogj

Reputation: 161

Do you really require a separate entity? What I am getting at is why not create an anonymous object which returns the entities required or just the properties required. A potential problem may occur by having an entity for every data scenario.

Upvotes: 0

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

You can make a view Model and put everything you need in View in this viewmodel.

public class MyViewModel
{
     Entity1 Ent{get;set;}
     Entity2 Ent2{get;set;}
     DateTime CurrentDate{get;set;}  
}

public ActionResult index()
{
    MyViewModel model = new MyviewModel();
    model.Ent = new Entity1();
    model.Ent2 = new Entity2();
    model.CurrentDate = DateTime.Now;
    return View(model) 
}

your view must now accept VieModel instead of db generated entity. in view you can access entities like

<%:Model.Ent1.SomeProperty%>
<%:Model.CurrentDate%>
<%:Model.Ent2.SomeProperty2%>

Upvotes: 1

rob waminal
rob waminal

Reputation: 18419

What I would do is to create a ViewModel that will only hold the information specific for a View, not the whole entity.

Upvotes: 0

Related Questions