locked
Entity Framework RRS feed

  • Question

  • Hi,

    According to http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx, I created a sample project:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    namespace CodeFirstSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var db = new ProductContext())
                {
                    // Add a food category        
                    var food = new Category { CategoryId = "FOOD", Name = "Foods" };
                    db.Categories.Add(food);
                    int recordsAffected = db.SaveChanges();
                    Console.WriteLine("Saved {0} entities to the database, press any key to exit.", recordsAffected);
                    Console.ReadKey();
                }
            }
        }
        public class Category
        {
            public string CategoryId { get; set; }
            public string Name { get; set; }
            public virtual ICollection<Product> Products { get; set; }
        }
        public class Product
        {
            public int ProductId { get; set; }
            public string Name { get; set; }
            public string CategoryId { get; set; }
            public virtual Category Category { get; set; }
        }
        public class ProductContext : DbContext
        {
            public DbSet<Category> Categories { get; set; }
            public DbSet<Product> Products { get; set; }
        }
    }

    Under the Category class there is  the following property:

    public virtual ICollection<Product> Products { get; set; }

    Can you please explain it and why it is needed.

    Regards,

    Bader

    • Moved by Mike Feng Friday, September 7, 2012 9:04 AM EF (From:.NET Base Class Library)
    Thursday, September 6, 2012 7:20 AM

Answers

  • I did not got a chance to look in to the URL but, from the source code it says , There is a one -> many relationship between category and product entity.


    Lingaraj Mishra

    • Marked as answer by Nadim2522 Monday, September 10, 2012 11:09 AM
    Thursday, September 6, 2012 12:57 PM
  • Hi Bader,

    ICollection<T> is an interface that exposes collection semantics such as Add(), Remove(), and Count. Collection<T> is a concrete implementation of the ICollection<T> interface.

    IList<T> is essentially an ordered ICollection<T>. List<T> is a concrete implementation of the IList<T> interface.

    In this case you should decide whether or not your results require list semantics such as ordering (then use IList<T>) or whether you just need to return an un-ordered collection (then use ICollection<T>).

    Hope I am able to clear your doubt :)


    Regards, http://www.shwetalodha.blogspot.in/

    • Proposed as answer by Shweta Jain (Lodha) Friday, September 7, 2012 3:41 AM
    • Marked as answer by Nadim2522 Monday, September 10, 2012 11:08 AM
    Friday, September 7, 2012 3:41 AM
  • To represent One -> Many relationship EF just need a IEnumerable type which can be ICollection,IList ….

    List<> and ICollection exposes collection semantics such as Add (), Remove (), and Count (). But ingeneral use IList<> instead of List<> as Returning an interface type is need to bew more general. That way you have greater freedom to change the implementation without breaking the code that is using it.

    Answering to your question, List<T> allows you to access elements via an index .i.e. Random access by index  where as ICollection<T> can only be accessed by enumerating them But EF by-default uses ICollection and it is suggested to use. A very good explanation is mentioned by Merlyn here: http://stackoverflow.com/questions/7655845/icollectiont-vs-listt-in-entity-framework


    Lingaraj Mishra


    • Edited by Lingaraj Mishra Friday, September 7, 2012 11:24 AM
    • Marked as answer by Nadim2522 Monday, September 10, 2012 11:11 AM
    Friday, September 7, 2012 11:23 AM

All replies

  • I did not got a chance to look in to the URL but, from the source code it says , There is a one -> many relationship between category and product entity.


    Lingaraj Mishra

    • Marked as answer by Nadim2522 Monday, September 10, 2012 11:09 AM
    Thursday, September 6, 2012 12:57 PM
  • Hi,

    Thank you very much,

    So what is the diffrence between the above code and the follwing code:

    public class Blog 
    { 
        public int BlogId { get; set; } 
        public string Name { get; set; }         public string Url { get; set; }  
        public int Rating { get; set; } 
        public List<Post> Posts { get; set; } 
    } 
      
    public class Post 
    { 
        public int PostId { get; set; } 
        [MaxLength(200)] 
        public string Title { get; set; } 
        public string Content { get; set; } 
      
        public int BlogId { get; set; } 
        public Blog Blog { get; set; } 
    }

    Here the "List" object is used instaed of "ICollection".

    Can you please explain to me the difference between the two codes?

    Regads,

    Bader

    Thursday, September 6, 2012 1:11 PM
  • Hi Bader,

    ICollection<T> is an interface that exposes collection semantics such as Add(), Remove(), and Count. Collection<T> is a concrete implementation of the ICollection<T> interface.

    IList<T> is essentially an ordered ICollection<T>. List<T> is a concrete implementation of the IList<T> interface.

    In this case you should decide whether or not your results require list semantics such as ordering (then use IList<T>) or whether you just need to return an un-ordered collection (then use ICollection<T>).

    Hope I am able to clear your doubt :)


    Regards, http://www.shwetalodha.blogspot.in/

    • Proposed as answer by Shweta Jain (Lodha) Friday, September 7, 2012 3:41 AM
    • Marked as answer by Nadim2522 Monday, September 10, 2012 11:08 AM
    Friday, September 7, 2012 3:41 AM
  • To represent One -> Many relationship EF just need a IEnumerable type which can be ICollection,IList ….

    List<> and ICollection exposes collection semantics such as Add (), Remove (), and Count (). But ingeneral use IList<> instead of List<> as Returning an interface type is need to bew more general. That way you have greater freedom to change the implementation without breaking the code that is using it.

    Answering to your question, List<T> allows you to access elements via an index .i.e. Random access by index  where as ICollection<T> can only be accessed by enumerating them But EF by-default uses ICollection and it is suggested to use. A very good explanation is mentioned by Merlyn here: http://stackoverflow.com/questions/7655845/icollectiont-vs-listt-in-entity-framework


    Lingaraj Mishra


    • Edited by Lingaraj Mishra Friday, September 7, 2012 11:24 AM
    • Marked as answer by Nadim2522 Monday, September 10, 2012 11:11 AM
    Friday, September 7, 2012 11:23 AM