0

I am wondering if every table that will be in a db needs to have a class representation in the EF?

For example:

I have a customer table and I have a class for that table.

I have an address table and I have a class for that table.

I have a Customer_Address_Mapping table that has two columns. One is customerid and the other is addressid. So obviously this table associates customers with their addresses. The fk relationships are in place.

So do I have to have a class for this table in the EF?

I'm sure at some point I'll have to write a LINQ query that will query the customer table and to get the addresses for said customer join to the Customer_Address_Mapping table....

So do I have to have a class that represents the Customer_Address_Mapping table to use in a LINQ join or is there a way / does EF do that heavy lifting for me....and if so what is that topic / subject as I need to educate myself.

TIA


For anyone else who comes across this after MarcinJuraszek educated me on navigation properties I found this to be a great follow up lesson.

http://blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first

GPGVM
  • 5,515
  • 10
  • 56
  • 97

1 Answers1

1

No, you don't have to have a class for mapping table. Just add navigation properties into Customer and Address classes and EF will take care of mapping table itself.

If you're using DB first approach with context generator it will be done for your automatically. If you're trying to create your entities all by yourself you can find that question and accepted answer helpful: MVC4 Entity Framework many-to-many custom join table name and schema name. It's about oracle, but the same applies to any other provider.

Update

Consider following POCO entities:

public class Foo
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public Bar Bar { get; set; }
}

public class Bar
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Foo> Foos { get; set; }
}

There is 1 to many relationship between Foo and Bar. To get Foo item with corresponding Bar you use Include:

using(var ctx = new MyContext())
{
    var results = cts.Foos.Include("Bar").Single(x => x.Id == 10);
}

It will add proper JOIN into SQL query.

To get particular Bar instance with Foo items with Name LIKE '%something%' things get a little more tricky, but you still don't need to add Join explicitly:

using(var ctx = new MyContext())
{
    var results = cts.Bar
                     .Select(b => new { Bar = b, Foos = b.Foos.Where(f => f.Name.Contains("Something")) })
                     .ToList();
}

EF is smart enough to know, that accessing b.Foos requires JOIN to be added.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • So how would my join look? See I asked this question a few days ago and I think I worded it so poorly (I'm new to this EF stuff) that it didn't get much attention. Anyway would I need to reference the mapping table in the join? SO question here: http://stackoverflow.com/questions/21748609/linq-join-to-mapping-table-with-ef – GPGVM Feb 14 '14 at 01:09
  • You don't do join at all. You just use navigation property and EF introduces `JOIN` into generated SQL query automatically. – MarcinJuraszek Feb 14 '14 at 01:10