Question

please have a look at the following POCOs:

public class Country
{
    [Key]
    public Guid ID { get; set; }

    [Required]
    public virtual Currency Currency { get; set; }
}

public class Currency1
{
    [Key]
    public Guid ID { get; set; }

    public virtual ICollection<Country> Countries { get; set; }
}

public class Currency2
{
    [Key]
    public Guid ID { get; set; }
}

I am not exactly sure what I need navigation properties like the ICollection in Currency1 for. If it comes to EF CodeFirst I see no difference in the database structure created. The tables of Currency1 and Currency2 look pretty much the same to me. So why or when does it make sense to add this extra property?

Of course, just thinking of the POCOs I understand that I can't access any countries from a Currency2 object. For example:

        var a = currency1.Countries;    // works fine
        var b = currency2.Countries;    // does not even compile

But is this the only difference? In other words: If I do not need to access countries from a Currency2 object, there is no need to add a corresponding navigation property in the Currency2 class for the purposes of EF? Kind of confused here...

Was it helpful?

Solution

Navigation properties are used either for direct access (as you described) or in linq-to-entities queries. If you don't plan to use it you can remove it from your model. Just be aware that you need a navigation property on at least one side to be able to model database realation using the code first approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top