Calculated Columns in the Entity Framework? -
i've not yet got experience orm's, , on last asp.net web forms site, friend created mapping file (for nhibernate) contained couple of calculated columns. these 2 columns mapped couple of properties 'photo' model.
now i'm re-creating same small website, albeit using asp.net mvc , far have used entity framework, however, need accomplish same thing, map these 2 calculated columns couple of properties 'photo' model, can't see how using framework.
here of content of original nhibernate mapping file photo object.
<class xmlns="urn:nhibernate-mapping-2.2" name="photo" table="photos"> <id name="id" unsaved-value="0"> <column name="photoid" /> <generator class="native" /> </id> <many-to-one name="lens" not-null="true"> <column name="lensid" /> </many-to-one> <property name="title" not-null="true"> <column name="phototitle" /> </property> <property name="description" not-null="true"> <column name="photodescrip" /> </property> <property name="totalphotosinmonth" formula="(select count(p.photoid) photos p p.datetimeposted between dateadd(month, datediff(month, 0, datetimeposted), 0) , dateadd(second, -1, dateadd(month, datediff(month, 0, datetimeposted) + 1, 0)))" /> <property name="totalphotosinyear" formula="(select count(p.photoid) photos p p.datetimeposted between dateadd(year, datediff(year, 0, datetimeposted), 0) , dateadd(second, -1, dateadd(year, datediff(year, 0, datetimeposted) + 1, 0)))" /> </class>
it's last 2 properties (totalphotosinmonth & totalphotosinyear) in mapping file i'm trying accomplish using entity framework (where want retrieve total number of photos in db month/year).
can please advise how can using entity framework?
you can try trick using definingquery:
<definingquery> select photoid, lensid, phototitle, photodescrip, (select count(p.photoid) photos p p.datetimeposted between dateadd(month, datediff(month, 0, datetimeposted), 0) , dateadd(second, -1, dateadd(month, datediff(month, 0, datetimeposted) + 1, 0))) totalphotosinmonth, (select count(p.photoid) photos p p.datetimeposted between dateadd(year, datediff(year, 0, datetimeposted), 0) , dateadd(second, -1, dateadd(year, datediff(year, 0, datetimeposted) + 1, 0))) totalphotosinyear photos </definingquery>
please note entity read-only unless specify insert, update , delete stored procedures it.
Comments
Post a Comment