Best appraoch to override my ICollection to be IList inside my edmx file, inside asp.net MVC web application

asp.net-mvc asp.net-mvc-4 edmx entity-framework-6

Question

I am working on an ASP.NET MVC web application. And I have mapped my database tables using ADO.NET Entity Framework, which generated a .edmx file. Now one of the limitations of the generated .edmx file, is that all the collections which represent a parent-child relation, will be defined as ICollection. For example I have this Question model class:-

public Question()
        {
            this.Anwers = new HashSet<Anwer>();
        }

        public int Id { get; set; }
        public string QuestionDesc { get; set; }
        public int Order { get; set; }
        public bool Active { get; set; }

        public virtual ICollection<Anwer> Answers { get; set; }

Here is the answer model:-

  public partial class Anwer
    {
        public Anwer()
        {
            this.UserFormsAnswers = new HashSet<UserFormsAnswer>();
        }

        public int Id { get; set; }
        public string AnwerDesc { get; set; }
        public int QuestionID { get; set; }
        public bool Correct { get; set; }

        public virtual Question Question { get; set; }
        public virtual ICollection<UserFormsAnswer> UserFormsAnswers { get; set; }
    }

Now the problem with the ICollection compared to IList<> is that I can not use indexer to get the values of the Answers related to a question. While if I have IList<> instead of ICollection<> I can indexing the Answers for a Question.

So my question is what is the best appraoch to override my public virtual ICollection<Anwer> Answers { get; set; } to be public virtual IList<Anwer> Answers { get; set; }? Of course modifying the automatically generated .edmx file should be avoided as my modifications will be overridden if I remap my database tables..

1
2
6/22/2017 2:11:58 AM

Popular Answer

If you truly need to modify all of your ICollection objects into IList objects then the correct place to do that is in your T4 templates. They're the ".tt" files inside of your edmx. Those are the templates which are used to generate the code files that the rest of your project uses. Any modifications made to those templates will be propagated through your project any time you update your edmx.

That said, I'd question whether or not you actually need to make this change. You're much better off keeping the return type from your database as generic as possible, and then narrowing it down to a List (or whatever) where you're going to use it.

6
6/9/2017 9:57:14 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow