none
how to save ICollection navigation propoties in MVC RRS feed

  • Question

  • In .NET MVC Project, url is http://msdn.microsoft.com/en-us/data/gg685494

    the Blog class properties are-  tring BloggerName { get; set; }

    ICollection<Post> Posts { get; set; } //navigation property, Post is another class-

    but after create the database, in the Blog table, we can only see the Id and BloggerName field

    there is no posts -this 'field' in table, so after we do create, insert multiple posts for this blog row, how can we save these posts navigation data? and later where to retrieve these posts data from Blog table if we never saved it in database, where they are?

    hope to get your answer, Thanks, Julia

    • Moved by Andrew.Wu Monday, October 17, 2011 8:53 AM (发件人:Visual C#)
    • Moved by Sheng Jiang 蒋晟 Monday, October 17, 2011 6:34 PM English (From:ADO.NET 与 LINQ)
    Thursday, October 13, 2011 7:30 PM

Answers

  • It depends which data access framework you were using to persist the Blog and Post tables.

    If you are using Entity Frramework 4.1, then the Code-First feature enables you to automatically persist any navigation properties within an entity. Like this:

    [Table]
    public class Blog
    {
        public virtual ICollection<Post> Posts { get; set; }
    }

    [Table]
    public class Post { ... }

    using (MyDbContext context = new MyDbContext())
    {
        Blog b = new Blog { BlogName = "...", Posts = new[] { new Post { ... } } };
        context.Blog.Add(b);
        context.SaveChanges();
    }

    if you were using another framework, like EF 4.0 or LINQ to SQL, then this will be a little bit complicated. explicitly add each of the entity to the corresponding entity set, then submit the changes to the object context.


    Mark Zhou
    Friday, October 14, 2011 7:07 AM
  • The EntityFramework sees Blog and Post table has relation when you do steps in figure 6, so it create that Navigation property based on your command.

    You just add new Blog to Blogs and call db.SaveChanges() when you're done, and the Blog "row" will go to the Blog "table".

    Friday, October 14, 2011 7:19 AM
  • Hi juliaji,

    As @Chenong00 suggested, make sure your association setting is right:

    If the Blog Class like this:

    public partial class Blog
        {
            public Blog()
            {
                this.Posts = new HashSet<Post>();
            }
            public int Id { get; set; }
            public string Title { get; set; }
            public string BloggerName { get; set; }
            public virtual ICollection<Post> Posts { get; set; }
        }
    
    

    The instance must could see Posts perperty, it is pulic.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 20, 2011 7:35 AM
    Moderator

All replies

  • In .NET MVC Project, url is http://msdn.microsoft.com/en-us/data/gg685494

    the Blog class properties are-  tring BloggerName { get; set; }

    ICollection<Post> Posts { get; set; } //navigation property, Post is another class-

    but after create the database, in the Blog table, we can only see the Id and BloggerName field

    there is no posts -this 'field' in table, so after we do create, insert multiple posts, how can we save this posts navigation data? and later where to retrieve these posts data from Blog table if we never saved it in database, where they are?

    hope to get your answer, Thanks,
    • Merged by Paul Zhou Tuesday, October 18, 2011 5:59 AM the same question
    Thursday, October 13, 2011 8:20 PM
  • It depends which data access framework you were using to persist the Blog and Post tables.

    If you are using Entity Frramework 4.1, then the Code-First feature enables you to automatically persist any navigation properties within an entity. Like this:

    [Table]
    public class Blog
    {
        public virtual ICollection<Post> Posts { get; set; }
    }

    [Table]
    public class Post { ... }

    using (MyDbContext context = new MyDbContext())
    {
        Blog b = new Blog { BlogName = "...", Posts = new[] { new Post { ... } } };
        context.Blog.Add(b);
        context.SaveChanges();
    }

    if you were using another framework, like EF 4.0 or LINQ to SQL, then this will be a little bit complicated. explicitly add each of the entity to the corresponding entity set, then submit the changes to the object context.


    Mark Zhou
    Friday, October 14, 2011 7:07 AM
  • The EntityFramework sees Blog and Post table has relation when you do steps in figure 6, so it create that Navigation property based on your command.

    You just add new Blog to Blogs and call db.SaveChanges() when you're done, and the Blog "row" will go to the Blog "table".

    Friday, October 14, 2011 7:19 AM
  • Since this is a one-to-many relationship, a Blog field will be created in the Post table, not the other way around.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, October 17, 2011 6:36 PM
  • I know the

        context.Blog.Add(b);
        context.SaveChanges();
    is to save data,

    but the database Blog table have only ID and BlogName 2 field –you can see from database,

    there is no this field- ICollection<Post> Posts- you can’t see this ‘field’ in database table.!

     

     so how and where you can save the Posts to database Blog table ?

    The ICollection<Post> Posts only is class property, not table field ,

    so later you check the Blog table You can only see ID and BlogName 2 field,

     where to find the ICollection<Post>Posts- data that you SaveChanges();?
    in database table in some where?

    Thanks lot!

    Wednesday, October 19, 2011 8:23 PM
  • I know the

       context.Blog.Add(b);
        context.SaveChanges();
    is to save data,

    but the database Blog table have only ID and BlogName 2 field –you can see from database, there is no this field- ICollection<Post> Posts- you can’t see this ‘field’ in database table.!

    so how and where you can save the Posts? -the navigation data ?

    The ICollection<Post> Posts only is class property, not table field, it can be save to database table?

    so later you check the Blog table You can only see ID and BlogName 2 field,

    where to find the ICollection<Post>Posts- data that you SaveChanges(); ?  in database table?

    Thanks lot!

    Wednesday, October 19, 2011 8:27 PM
  • I know the

       context.Blog.Add(b);
        context.SaveChanges();
    is to save data, a

    nd you can save ID and BlogName 2 field

    todatabase Blog table -you can see from database,

    but there is no this field- ICollection<Post> Posts- you can’t see this ‘field’ in database table.!

    so how and where you can save the Posts? -for the navigation data ?

    The ICollection<Post> Posts only is class property, not table field, it can be save to database table?

    so later you check the Blog table You can only see ID and BlogName 2 field,

    where to find the ICollection<Post>Posts- data that you SaveChanges(); ?  in database table?

    Thanks lot!

    Wednesday, October 19, 2011 9:03 PM
  • context.Blog.Add(b);

    b.Posts.Add(p);

    context.SaveChanges();

    That is, if your navigation property is set up property. Either you need to have a Blog navigation property on Post (HasRequired/WithMany) or you need to change the entity configuration to add WithOptionalDependent 



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Wednesday, October 19, 2011 9:50 PM
  • Hi juliaji,

    As @Chenong00 suggested, make sure your association setting is right:

    If the Blog Class like this:

    public partial class Blog
        {
            public Blog()
            {
                this.Posts = new HashSet<Post>();
            }
            public int Id { get; set; }
            public string Title { get; set; }
            public string BloggerName { get; set; }
            public virtual ICollection<Post> Posts { get; set; }
        }
    
    

    The instance must could see Posts perperty, it is pulic.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 20, 2011 7:35 AM
    Moderator
  • * For Blog-      public virtual ICollection<Post> Posts { get; set; }

    for example-      Blog b = new Blog(){Id = 1, BlogName= “name1”};

    Post p3 = new Post(){Id= 3; Name = “pName3”};

    Post p4 = new Post(){Id = 4; Name =”pName4”}:

    b.Posts.Add(p3);        b.Posts.Add(p4);

    after call-            context.Blog.Add(b);
          context.SaveChanges();

    in database Blog table you will save  Id=1 and BlogName=”name1” 2 fields in this record,

    but where to save Blog’s Posts 2 records [p3 and p4]? The Blog table have no Posts this ‘field’!

    and later when you get Blot Id = 1 this record, you can see the BlogName=”name1

    but how do you find the 2 Posts records for this record?

    Thanks

    Thursday, October 20, 2011 8:42 PM
  • Why you expect the blog table to have a post field? You can't. A table row can only handle one record. Did you look up the post table as I suggested earlier?

    To get navigation property loaded automatically, you need to turn off lazy loading. By default lazy loading is on to improve performance.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, October 20, 2011 8:56 PM
  • Hi,

    Your code is right now, your question is where to find the Posts records, right? Actually there are two tables (Blogs and Posts) in your database and the Blogs table is the parent table(foreign key), the post records should in Posts table.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 21, 2011 1:46 AM
    Moderator