开发者

How to include another table in MVC3 with EF ? retrieving data into list

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 =开发者_如何学C 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


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();


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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜