Code First: Independent associations vs. Foreign key associations?

前端 未结 4 1170
醉梦人生
醉梦人生 2020-11-22 01:07

I have a mental debate with myself every time I start working on a new project and I am designing my POCOs. I have seen many tutorials/code samples that seem to favor fo

4条回答
  •  孤独总比滥情好
    2020-11-22 01:32

    Use both. And make your entity references virtual to allow for lazy loading. Like this:

    public class Order
    {
      public int ID { get; set; }
      public int CustomerID { get; set; }
      public virtual Customer Customer { get; set; } // <-- Customer object
      ...
    }
    

    This saves on unnecessary DB lookups, allows lazy loading, and allows you to easily see/set the ID if you know what you want it to be. Note that having both does not change your table structure in any way.

提交回复
热议问题