0

Consider below code:

namespace Domain.Models;

public partial class Class1
{
    //Other properties

    [IgnoreDataMember]
    public virtual ICollection<Activity> Activities { get; set; }
}
public partial class Class2
{
    //Other properties

    [IgnoreDataMember]
    public virtual ICollection<Activity> Activities { get; set; }
}

adding [IgnoreDataMember] to public virtual ICollection<...> in each class, is a repetitive task (Consider the large number of these repetitive tasks in a large project)!

Is there any clean solution that works like this proposition: "Add [IgnoreDataMember] to all virtual ICollection properties"?

nima ansari
  • 434
  • 9
  • 19

1 Answers1

1

You can use VScode and use edit-"replace in files" function. Just note that visual studio cannot replace with multiple line.
![enter image description here

And in case you add depulicate attribute. You can then replace the double attribute to one.
enter image description here
![enter image description here

Qiang Fu
  • 1,401
  • 1
  • 2
  • 8
  • I agree that this is the way to do it, but i disagree with the 'cannot replace with multiple line' statement. It is possible to do so by using Regex ( .* Icon) – Xavjer May 05 '23 at 12:08
  • @Xavjer I think you are right. Regex could be a better solution because different spaces width may cannot be recognized by this simple replace. – Qiang Fu May 05 '23 at 12:16
  • 1
    I think for the question, the normal replace will work best as long as everything is 'private virtual ICollection.....', maybe a reformat will be necessary for the indentation but I would prefer to do it this way and omit having to write a Regex just to have the spaces correct afterwards :) – Xavjer May 05 '23 at 12:22
  • @QiangFu Thanks for you answer. It's a solution, but I want to implement it in Code. e.g. a setting in startup that says: "ignore all virtual ICollection data members". because base on you solution, after adding new model, I should replace it or add that attribute one another time(It's also a repetitive task)! – nima ansari May 07 '23 at 10:34
  • @nimaansari Does this help? https://stackoverflow.com/questions/24597120/how-to-ignore-all-properties-that-are-marked-as-virtual – Qiang Fu May 15 '23 at 06:57