开发者

Exact purpose of Entity Framework navigation properties

please have a look at the following POCOs:

public class Country
{
    [Key]
  开发者_Go百科  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...


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜