Entity framework 实体框架中的计算列?

Entity framework 实体框架中的计算列?,entity-framework,entity-framework-4,Entity Framework,Entity Framework 4,我还没有太多的ORM经验,在我上一个ASP.NET Web表单网站上,一个朋友为nHibernate创建了一个映射文件,其中包含两个计算列。这两列映射到我的“照片”模型的几个属性 现在,我正在创建同一个小网站,虽然使用ASP.NET MVC,并且到目前为止已经使用了实体框架,但是,我需要完成同样的事情,将这两个计算列映射到我的“照片”模型的几个属性,但是看不到如何使用此框架 下面是我的原始nHibernate映射文件中有关照片对象的一些内容 <class xmlns="urn:nhiber

我还没有太多的ORM经验,在我上一个ASP.NET Web表单网站上,一个朋友为nHibernate创建了一个映射文件,其中包含两个计算列。这两列映射到我的“照片”模型的几个属性

现在,我正在创建同一个小网站,虽然使用ASP.NET MVC,并且到目前为止已经使用了实体框架,但是,我需要完成同样的事情,将这两个计算列映射到我的“照片”模型的几个属性,但是看不到如何使用此框架

下面是我的原始nHibernate映射文件中有关照片对象的一些内容

<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) FROM Photos p WHERE p.DateTimePosted BETWEEN DATEADD(month, DATEDIFF(month, 0, DateTimePosted), 0) AND DATEADD(second, -1, DATEADD(month, DATEDIFF(month, 0, DateTimePosted) + 1, 0)))" />
<property name="TotalPhotosInYear" formula="(SELECT COUNT(p.PhotoId) FROM Photos p WHERE p.DateTimePosted BETWEEN DATEADD(year, DATEDIFF(year, 0, DateTimePosted), 0) AND DATEADD(second, -1, DATEADD(year, DATEDIFF(year, 0, DateTimePosted) + 1, 0)))" />
 </class>
这是这个映射文件中最后两个属性TotalPhotosInMonth和TotalPhotosInYear,我最终尝试使用实体框架来完成,我只想检索数据库中该月/年的照片总数


有人能告诉我如何使用实体框架来实现这一点吗?

您可以尝试使用以下方法:


请注意,除非您为该实体指定了插入、更新和删除存储过程,否则该实体将是只读的。

您可以尝试使用以下方法执行此操作:

请注意,除非您为该实体指定了插入、更新和删除存储过程,否则该实体将是只读的

<DefiningQuery>
  SELECT photoid, lensid, phototitle, photodescrip, (SELECT COUNT(p.PhotoId) FROM Photos p WHERE p.DateTimePosted BETWEEN DATEADD(month, DATEDIFF(month, 0, DateTimePosted), 0) AND DATEADD(second, -1, DATEADD(month, DATEDIFF(month, 0, DateTimePosted) + 1, 0))) as totalphotosinmonth, (SELECT COUNT(p.PhotoId) FROM Photos p WHERE p.DateTimePosted BETWEEN DATEADD(year, DATEDIFF(year, 0, DateTimePosted), 0) AND DATEADD(second, -1, DATEADD(year, DATEDIFF(year, 0, DateTimePosted) + 1, 0))) as totalphotosinyear from photos
</DefiningQuery>