Change LINQ2SQL property in partial class?
I have a table in my LINQ2SQL diagram with a DayOfWeek column in the JourneyBooking table. The .cs constructor has a definition of this as:
[Column(Storage="_DayOfWeek", DbType="TinyInt NOT NULL")]
public int DayOfWeek
{
get
{
return this._DayOfWeek;
}
set
{
if ((this._DayOfWeek != value))
{
this.OnDayOfWeekChanging(value);
this.SendPropertyChanging();
this._DayOfWeek = value;
this.SendPropertyChanged("DayOfWeek");
this.OnDayOfWeekChanged();
}
}
}
I have a partial class for the JourneyBooking class. Is it possible to extend / overwrite the above DayOfWeek property with its own property?
The problem I'm running into is that DayOfWeek has a value of 0-6 in C # but 1-7 in SQL, so I thought I could overwrite the DayOfWeek property to subtract or add 1 depending on whether got it or set it, is it possible or will I need to rewrite all the code above in my partial class?
a source to share
You can much more easily write another property in your partial that just references that in get / set, and then just use that property in your code. For example, I would rename the generated SQL code like DayOfWeekStorage
or DayOfWeekSQL
, and then do it in partial:
public int DayOfWeek
{
get { return DayOfWeekStorage -1; }
set { DayOfWeekStorage = value + 1; }
}
a source to share
No, with LINQ to SQL there is no real good way to do this.
You can create a property DayOfWeekStorage
(as Nick Craver explained), make it internal, and define a new property WeekDay DayOfWeek
. It will even compile when using this new property in a LINQ query. However, this LINQ query will fail at runtime because LINQ to SQL has no idea how to convert your query to LINQ to SQL. This is where LINQ to SQL stops.
Creating a new property is possible if you have no intention of using it in a LINQ query (so there is no filtering, grouping and ordering based on that property), but it's easy to do it by accident and find out at runtime.
a source to share
You can check this problem / solution. I asked this type of problem before:] Problem with implementing interface in partial classes hope this helps!
a source to share