1

I have a table called Email Table and I have a table called ParentEmail Table which contains a column called ParentID.

I want to include ParentEmail in Parent table when I make a list. May I know how to do it ?

This didn't work:

var parent = db.parent.include("email").tolist();

Does anyone know how to include this kind of table structure?

Parent

parentID
Username
Password
Firstname
....

Email

EmailID
Email
ParentID
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1myb
  • 3,536
  • 12
  • 53
  • 73

4 Answers4

2

Do you mean something like this:

var query= from x in db.Email
             join y in context.Parent
             on x.ParentID equals y.ParentId
             select new { Email = x.Email , UserName = y.Username };

var list = query.ToList();
Peyman
  • 3,068
  • 1
  • 18
  • 32
  • exactly, but may i know if i use this way, how do i return this and parent to view ? (possible to include in parent var ? – 1myb Aug 04 '11 at 04:31
  • then what should i do ?. please guide -D – 1myb Aug 04 '11 at 04:38
  • but the thing is how to convert this into linq query ? because i have no idea on how to use this in mvc >.< i'm totally new in MVC – 1myb Aug 05 '11 at 00:39
  • @Spencer: I didn't get what's your mean? This sample is Linq query. Can you explain more? – Peyman Aug 05 '11 at 02:21
  • @peyman i meant i can't understand the row of select new {Email = x.email, username = y.username} when i pass to view with view(query); i can't use this modelItem => item.?? – 1myb Aug 05 '11 at 05:09
2

Make sure that your capitalization matches the model inside your Include statement, it will fail otherwise. I would HIGHLY suggest getting the EntityFramework 4.1 update from Nuget, it will add a strongly typed Include extension method. After you install that package add a reference to system.data.entity and you will be able to say something like

    var parent = db.parent.include(parent => parent.Email).tolist();

If you are still having trouble can you post a screenshot of your edmx file?

Jack Woodward
  • 455
  • 4
  • 20
  • Praveen, you can do joins like peyman showed and it will end up being the same sql as if you did a .include – Jack Woodward Aug 04 '11 at 05:01
  • 1
    @peyman was showing you how to select something called an anonymous type when he put in new {Email = x.email, username = y.username}. It is like creating a mini class in the middle of your method that you only use inside that method. I think in your case you should just use the Include function if the email table and the parent table are linked by a foreign key. I would comment down on the other answer but I don't have enough rep yet. – Jack Woodward Aug 06 '11 at 03:22
  • thx for reply, dude =D my email table linked to parent but parent din link back >.< i din set a column for parent to keep emailid but i do make a column for email table to link parent id =D – 1myb Aug 06 '11 at 17:36
  • 1
    if you are trying to say a parent can have many emails then the way you have the links(foreign keys) is correct. Don't link back by putting an email id on the parent unless a parent can only have one email. no prob. – Jack Woodward Aug 06 '11 at 18:06
1

add a property in Parent named Email

public class Parent
{
    [key]
    public string parentID {get;set;}
    public string Username {get;set;}
    public string Password {get;set;}
    public string Firstname {get;set;}

    public Email Email{get;set;}
}

public class Email
{
    public string EmailID {get;set;}
    public string Email {get;set;}
    public string ParentID {get;set;}
}


 public partial class MyDbContext:DbContext
{
      public DbSet<Parents> Parents{ get; set; }
      public DbSet<Email> Emails{ get; set; }
}

var db= new MyDbContext();
var parent = db.Parent.include("Email").tolist();

http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
  • just adding a property won't work. EF needs to know how to eager load it – Eranga Aug 04 '11 at 04:30
  • no `Parent` does not have a FK to `Email`. So how does EF figure out which email to load? – Eranga Aug 04 '11 at 04:35
  • i got this error >.< Member modifier 'public' must precede the member type and name for this line --public string public Email Email{get;set;} – 1myb Aug 04 '11 at 15:11
  • actually i got the database and i got the entity framework edmx, is this applicable for it ? i tried to work on, i failed >. – 1myb Aug 04 '11 at 16:13
  • 1
    open edmx file and select parent entity ,hold mouse and drag mouse to email entity, it will ask for relation type, choose the proper relation, edmx will automatically generate relation in back-end xml file. – Praveen Prasad Aug 04 '11 at 16:42
0

Finally i was get my solution from one members of this page

ASP/MVC3 Razor Linq

Sorry and Thank you for every one who replied me =D Thank you

Community
  • 1
  • 1
1myb
  • 3,536
  • 12
  • 53
  • 73