How can I extend the LINQ-to-SQL class without having to make changes every time the code is generated?


Update from comment:

I need to extend linq-to-sql classes according to my own parameters and do not want to touch any generated classes. Any best offers are welcome. But I also don't want to do all the attribute assignments all the time again if the linq-to-sql classes change. therefore, if vstudio generates a new attribute for the class, I have my own extended attributes, which are stored separately, and a new one, placed from the class itself


The original question:

I am not sure if this is possible. I have a cool car and a T-shirt class, released from a car class. The mycar class also has a string list. Just the difference.

How can I now transfer any car object to mycar without having to assign all the attributes each manually. How:

Car car = new Car();

MyCar mcar = (MyCar) car;

or

MyCar mcar = new MyCar(car);

or, nevertheless, I can expand the car with my own variables and do not have to do

Car car = new Car();
MyCar mcar = new MyCar();
mcar.name = car.name;
mcar.xyz = car.xyz;
...

Thank.

+3
source share
4 answers

In response to your comment on the question, Linq-To-Sql classes are generated as partial. This means that you can have separate code files with the same class names that were declared partial to add additional properties that you want.

eg. Your Ling-To-Sql design class will look like this:

public partial class Car
{
     .... lots of auto generated stuff ....
}

You can have your own separate file (in the same project) called Car.cs:

public partial class Car
{
     public MyCustomStuff{ get; set; }
}

. , .

+16

, :

class MyCar {
    public MyCar(Car car) {
        name = car.name;
        // etc
    }
}
+2

Car MyCar, , Car MyCar. , , as, null, .

Car? MyCar ? , Car. MyCar Car? MyCar ?

MyCar, Car MyCar Car .

MyCar Car

+1

:


  • public static explicit operator Car(MyCar source)


  • public MyCar(Car source)

  • public static class CarExtensions {
    public static MyCar Create(this Car source) }

0
source

Source: https://habr.com/ru/post/1744758/


All Articles