Changing the value of a property in a partial class from another property in the same partial class

46 Views Asked by At

I am working with an existing database that can change at any time, the columns' names in the tables may change and new columns may be added at any time.

I DO NOT want to be obliged to change my code in case of any changes in the column names.

Thats why I followed the approach of migrating the tables into partial classes.

I followed this approach so that in case of any change of columns' names, I can still access my columns in my code using fixed column names that I defined in the second file of my partial classes' implementation.

The Structure of the folders is as follows:

enter image description here

The classes files in Models/Generated were generated using the Scaffold command:

Scaffold-DbContext "Data Source=DESKTOP-XXX\SQLEXPRESS01;Initial Catalog=Pluto_Db_First;Integrated Security=True;Encrypt=False;" Microsoft.EntityFrameworkCore.SqlServer -ContextDir Data -OutputDir Models/Generated -DataAnnotation -ContextNamespace PlutoDbFirst.Data -Namespace PlutoDbFirst.Models

As you can see in the image the second file of the partial class "Course" implementation is in the folder "Models"

The code inside this class is: (I wrote this code)

public partial class Course
{
    public string courseTitle
    {
        get => $"{Title}";
    }


    public string bestStudent
    {
        get => $"{BestStudent}";
    }
}

The code inside the class "Course" in Models/Generated is as follows: (generated by Scaffold-DbContext)

public partial class Course
{
    [StringLength(255)]
    [Unicode(false)]

    public string Title { get; set; } = null!;


    [Column("Best_Student")]
    [StringLength(50)]
    [Unicode(false)]

    public string? BestStudent { get; set; }
}

The good thing is that now I can view any column I want using the properties I defined in my implementation of the partial class.

for example I can write in Program.cs:

using PlutoDbFirstContext contextDb = new PlutoDbFirstContext();
           
foreach(Course c in contextDb.Courses)
{
      Console.WriteLine("Course Title: " + c.courseTitle + "\t" + "Best Student Name: " + c.bestStudent);
}

The problem is:

I cannot assign values to the properties "Title" and "BestStudent" using the properties I created "courseTitle" and "bestStudent"

Note:

I found a similar question on stackoverflow from 2014, but the answers were not clear and some of the provided solutions did not work with me.

I tried to add a "set" accessor in my implementation for the variables "courseTitle" and "bestStudent" in order to pass this value to the properties "Title" and "BestStudent" so that I can save the changes to the database but I keep getting this long message error which starts with:

"Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'bestStudent'."

My implementation for adding "set" accessor:

public partial class Course
{
    public string courseTitle
    {
        get => $"{Title}";
    }


    public string bestStudent
    {
        get => $"{BestStudent}";
        
        set
        {
            BestStudent = value;
        }
    }
}

How can I set a value to the properties in the class "Course" in Models/Generated???

So that in case of any change I just "ReScaffold" and modify the name in my implementation of "Course" in Models and I do not have to modify my main code.

I followed this tutorial, but it did not provide any guidance on my question:

text

0

There are 0 best solutions below