none
Simple question: Database First, change tracking? RRS feed

  • Question

  • I have a database with two tables: Blogs and Posts. I create a model from this database and the entities look like this:

         public partial class Post
        {
            public int ID { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }
            public int BlogID { get; set; }
        
            public virtual Blog Blog { get; set; }
        }

    And similar for Blog.

    Why is the following happening?

                testingEntities entities = new testingEntities();
    
                Blog newBlog = entities.Blogs.Create();
                Post newPost = entities.Posts.Create();
    
                newPost.Blog = newBlog;
    
                //Why are the following integers 0??
                int count = newBlog.Posts.Count; // returns 0
                int id = newPost.BlogID; //returns 0

    Why does the count and id return 0?

    Thanks

    Friday, November 21, 2014 6:17 PM

Answers

  • All you did was was create some objects that have not been populated with data. So newBlog.Posts.Count is going to be 0, ybecuase you don't have any Post objects in the collection.

    Anytime you create an object new and it has numeric properties, the properties are going to be set to 0 until the properties are populated.

    It seems to me that you should be using a better tutorial.

    Friday, November 21, 2014 6:54 PM

All replies

  • All you did was was create some objects that have not been populated with data. So newBlog.Posts.Count is going to be 0, ybecuase you don't have any Post objects in the collection.

    Anytime you create an object new and it has numeric properties, the properties are going to be set to 0 until the properties are populated.

    It seems to me that you should be using a better tutorial.

    Friday, November 21, 2014 6:54 PM
  • Thanks for your reply.

    I thought the dynamic proxies generated by the Create() method were meant to handle foreign key tracking and collection handling for you?

    http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx
    Friday, November 21, 2014 9:42 PM
  • http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx

    I can say that I have never used it in EF using the DB first approach. I use the DB first exclusively when creating enterprise level solutions use EF. And I don't use POCO(s) either. I use DTO(s) that one loads manully in code mapping EF entity to DTO and vice versa. I use what's in the link for DB first.

    https://visualstudiogallery.msdn.microsoft.com/655aa6d4-4461-42ea-aeec-64cdb1313de7

    I don't know what this proxy thing you are doing is about.

    Friday, November 21, 2014 10:09 PM