Code First adding to collections? How to use Code First with repositories?

前端 未结 6 1324
执笔经年
执笔经年 2020-12-17 14:58

EDIT: This happen only on larger scale projects with repositories. Is there anybody using EF4 with CodeFirst approach and using repositories? Please advise me.

Hi. I

6条回答
  •  無奈伤痛
    2020-12-17 15:38

    Try use this signature. I hope this worked.

    public class Author
    {
        public virtual int AuthorId { get; set; }
        public virtual string Name { get; set; }
    
        private ICollection _books;
    
        public virtual ICollection Books
        {
            get { return _books ?? (_books = new HashSet()); } // Try HashSet
            set { _books = value; }
        }
    
        public void AddBook(Book book)
        {
            book.Author = this;
            Books.Add(book);
        }
    }
    

提交回复
热议问题