Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello Freind
i have a question . i searched in google for that but i Could not find a specific reply.
i am new on Ef.
i want to do a thing like following picture.
http://upload7.ir/viewer.php?file=13346259503430023518.jpg
indeed i want a thing like Natural Join Or making A view in sql.(similar to view)
i want to do it by Ef in (Model_First)and(Code_first) ways to understand How do it.
i have read and know Configure Relationships (One-to-One | one-to-Many | many to many)
between two Tables but i Do not Know how to do that with more two TABLES in EF.
i need a sample that Explain Detail of that and doing Crud operation on that table.

thanks a lot.
Posted

1 solution

It looks like you're trying to make EF behave like SQL, which really isn't its purpose. Many of the concepts that are fundamental to running SQL as a data provider service, such as views and stored procedures, do not have much bearing on how EF works. EF can use those things, and your program can emulate these things, but it will not specifically duplicate these things.

The virtual keyword is largely how you declare relationships.

Natural joins are easy. One to many looks like:

C#
public class Foo
{
    public int Id { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
}

public class Bar 
{
    public int Id { get; set; }

    [ForeignKey("Foo")]
    public int fooId{ get; set; }

    public virtual Foo Foo { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
}

EDIT:

I forgot to mention, you can invoke the property-based joins as a standard property, such as:
C#
Foo myFoo = context.Foos.First(//find the foo I'm looking for);
List<Bar> myBar = myFoo.Bars;



M:M :

C#
public class Foo
{
    public int Id { get; set; }

    public virtual ICollection<FooBar> FooBar { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public virtual ICollection<FooBar> FooBar { get; set; }
}

public class FooBar
{
    [ForeignKey("Foo")]
    public int fooId { get; set; }
    [ForeignKey("Bar")]
    public int barId { get; set; }
    
    public virtual Foo Foo { get; set; }
    public virtual Bar Bar { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
    public DbSet<FooBar> FooBars { get; set; }
}


Now for your view question. By and large, the way that you replicate views in EF is to create ViewModels, which are POCOs that are not mapped to the database but can be constructed from either POCO classes or, if you're fancy, from the DbContext.

C#
public class FooBarViewModel
{
    public List<string> FooProp1 { get; set; }
    public List<string> BarProp1 { get; set; }

    public FooBarViewModel(MyContext context)
    {
        FooProp1 = context.Foos.Select(x => x.Prop1).toList();
        BarProp1 = context.Bars.Select(x => x.Prop1).toList();
    }
}

public class Foo
{
    public int Id { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

public class Bar 
{
    public int Id { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
}


Hope this helps!
 
Share this answer
 
v4
Comments
mahmoodof 3-Sep-14 12:49pm    
hi
your reply was very good.
Can you give me some links to have more samples?pls send me.
thankyou so much.
Nathan Minier 3-Sep-14 12:54pm    
There are good primers in a couple places:
http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx

And the official set is a little obtuse sometimes, but is where you find the real meat:
http://blogs.msdn.com/b/webdev/archive/2013/11/01/tutorial-series-updated-for-entity-framework-6-code-first-with-mvc-5.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900