0

I'm migrating a project from EF core 2 to EF 7 when i found that in EF 7 after scaffolding, context models no longer have setters on virtual collection, example:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public ICollection<Post> Posts { get; } = new List<Post>();
}
public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PostedOn { get; set; }
    public Blog Blog { get; set; }
}

when inserting a new blog, I used to do it this way:

var blog = new Blog()
{
   Name = "",
   Author = "",
   Posts = new List<Post>() {
                    new Post(){Title="", Content = "" },
                    new Post(){Title="", Contect = "" },
                    new Post(){Title="", Contect = "" },
   }
};
_context.RetailLot.Add(blog);
_context.SaveChanges();

Apparently I cant do this anymore since it doesn't have a setter and my 2 options would be to add init keyword replacing the setter or keep it without a setter but manually add the posts by using someething like this

var blog = new Blog()
{
   Name = "",
   Author = ""
}
blog.Posts.Add(new Post(){Title="", Content = "" });
blog.Posts.Add(new Post(){Title="", Content = "" });
blog.Posts.Add(new Post(){Title="", Content = "" });

Are these indeed the only ways cause the second seems very manual and with adding init than force-scaffolding would replace it without using T4 templates...

Also I came across many instances where we fetch from context and we customize the select statement like below

_context.Blogs.include(x=>x.Posts).Select( b => new Blog(){
    Name = "MyBlog_"+x.Name,
    Author = x.Author,
    Posts = x.Posts
});

this is a simple example to how I needed to manipulate the returned object by selecting some specific columns to be returned or customize some columns. Posts=x.Posts will error out here because its a readonly. is adding init keyword my only acceptable option ? other workarounds is to create a new model that extend Blog but also override it to include a setter..

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
Mark Kordahi
  • 67
  • 10
  • There was a similar [question](https://stackoverflow.com/questions/75209985/is-it-possible-to-initialise-a-readonly-collection-property-using-a-collection-r) recently – Ivan Stoev Jan 27 '23 at 17:18
  • @IvanStoev i agree the linked question have some context related to this but doesn't clearly state what is the best way to move forward with this. specially second part of above question – Mark Kordahi Jan 27 '23 at 18:14
  • 1
    I didn't say it is exact duplicate, otherwise I would have simply closed it instead of commenting. However, as you can see from the link to GitHub, MSFT are not willing to fix it, because setter was violating some guidelines blah-blah, so you can clearly see there is no "best way to move forward". All they are suggesting is to modify the T4 template (which you are not willing to as I understand) and bring the setter back. So it's up to you to decide what's the best for you/what compromise you can do. – Ivan Stoev Jan 27 '23 at 20:25
  • Does this answer your question? [EF Core 6 to 7 the collections no longer have setters](https://stackoverflow.com/questions/74599304/ef-core-6-to-7-the-collections-no-longer-have-setters) – JHBonarius Jan 28 '23 at 10:15
  • Question asked before: [here](https://stackoverflow.com/questions/74869789) and [here](https://stackoverflow.com/questions/74599304) (which I tagged dupe, because it has answers) – JHBonarius Jan 28 '23 at 10:15

0 Answers0