0

How can I build a model using Entity Framework to join 3 tables?

At the moment I have:

public class KeywordAdCategory
{
    [Key]
    [Column("Keyword_Id", Order = 0)]
    public int Keyword_Id { get; set; }

    [Key]
    [Column("Ad_Id", Order = 1)]
    public int Ad_Id { get; set; }

    [Key]
    [Column("Category_Id", Order = 2)]
    public int Category_Id { get; set; }
}

But I don't have any navigation properties.

Is there a better way to build a relashionship between 3 tables using Entity Framework?

Also the Keyword, Ad and Category models:

public class Keyword
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
}

public class Ad
{
    // Primary properties
    [Key]
    public int Id { get; set; }

    // Navigation properties
    public AdOperation AdOperation { get; set; }
    public Member Member { get; set; }
    public Address Address { get; set; }
    public Category Category { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; }

    private ICollection<Feature> _features;
    public virtual ICollection<Feature> Features
    {
        get { return _features ?? (_features = new HashSet<Feature>()); }
        set { _features = value; }
    }
}

public class Category
{
    // Primary properties
    public int Id { get; set; }
    public int? CategoryParent_Id { get; set; }
    public int? CategoryGroup_Id { get; set; }
    public bool IsActive { get; set; }

    // Navigation properties
    public Keyword Keyword { get; set; }
}

Thanks.

1 Answer 1

1

I'm assuming that you're using Code-First Entity Framework here, and that you have your KeywordAdCategory object in your database as well. In which case, just simply do the following in your KeywordAdCategory class to do the proper mapping:

[Key, ForeignKey("Keyword")]
[Column("Keyword_Id", Order = 0)]
public int Keyword_Id { get; set; }

[Key, ForeignKey("Ad")]
[Column("Ad_Id", Order = 1)]
public int Ad_Id { get; set; }

[Key, ForeignKey("Category")]
[Column("Category_Id", Order = 2)]
public int Category_Id { get; set; }

public virtual Keyword Keyword { get; set; }

public virtual Ad Ad { get; set; }

public virtual Category Category { get; set; }

Doing this should do the proper mappings, put FKs on your KeywordAdCategory table, and thus give you the ability to have good navigation properties to the other objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks! You use the ForeignKey property to build a relashionship between the key and the Navigation Property? Regards
Yep. That's the beautiful thing with Entity Framework. You include one simple annotation like that and it will automatically populate the navigation properties when you call for that object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.