Free NHibernate mapping and / DatePart formulas

I have a very simple table with a Datetime column, and I have this mapping in a domain object.

MyDate is the name of the datetime column in the database.

public virtual int Day { get; set; }
public virtual int Month { get; set; }
public virtual int Year { get; set; }
public virtual int Hour { get; set; }
public virtual int Minutes { get; set; }
public virtual int Seconds { get;set; }
public virtual int WeekNo { get; set; }

Map(x => x.Day).Formula("DATEPART(day, Datetime)");
Map(x => x.Month).Formula("DATEPART(month, Datetime)");
Map(x => x.Year).Formula("DATEPART(year, Datetime)");
Map(x => x.Hour).Formula("DATEPART(hour, Datetime)");
Map(x => x.Minutes).Formula("DATEPART(minute, Datetime)");
Map(x => x.Seconds).Formula("DATEPART(second, Datetime)");
Map(x => x.WeekNo).Formula("DATEPART(week, Datetime)");

This works fine ... but Weekpartpart.

I saw the creation of sql for selection with NHProf and here the problem is, it generates all sql correctly, but in a week datepart .. this is part of the generated SQL:

.... Datepart (day, MyDate) ... .... Datepart (month, MyDate) ... .... Part date (year, MyDate) ... .... Part date (hour, MyDate ) ... .... Datepart (minute, MyDate) ... .... Datepart (second, MyDate) ... .... Datepart ( this_.week , MyDate) ...

where this_ is an alias for the table that nhibernate uses.

week datepart - . , , .

?

+3
2

. , :

(x = > x.WeekNo).Formula(@ "DATEPART (" "" ", -)" );

+1

, , , SQL Server.

Map(x => x.WeekNo).Formula("DATEPART(wk, Datetime)");

Map(x => x.WeekNo).Formula("DATEPART(ww, Datetime)");

.
: http://msdn.microsoft.com/en-us/library/aa258265(SQL.80).aspx

0

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


All Articles