开发者

does Entity framework support user defined functions as scalar properties

开发者 https://www.devze.com 2023-01-04 04:57 出处:网络
I would like to i开发者_运维技巧nclude the value of a scalar function as a read-only property of an entity, since that I could include that value in a filter, is such a thing possible?The solution is

I would like to i开发者_运维技巧nclude the value of a scalar function as a read-only property of an entity, since that I could include that value in a filter, is such a thing possible?


The solution is to add a DefiningQuery like in the following sample:


    <!-- SSDL content -->  
      <EntitySet Name="Emp" EntityType="TestModel.Store.Emp" >
        <DefiningQuery>
          SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, dbo.MyFunc(DEPTNO) AS DNAME FROM EMP
        </DefiningQuery>
      </EntitySet>
      <EntityType Name="Emp">
        <Key>
          <PropertyRef Name="EMPNO" />
        </Key>
        <Property Name="EMPNO" Type="int" Nullable="false" />
    ...
        <Property Name="DNAME" Type="varchar" MaxLength ="20" />
      </EntityType>
    ...
    <!-- CSDL content -->
    ...
        <EntityType Name="Emp">
          <Key>
            <PropertyRef Name="EMPNO" />
          </Key>
          <Property Name="EMPNO" Type="Int32" Nullable="false" />
    ...
          <Property Name="DNAME" Type="String" MaxLength="20" Unicode="false" FixedLength="false" />
        </EntityType>
    <!-- C-S mapping content -->
    ...
            <ScalarProperty Name="EMPNO" ColumnName="EMPNO" />
    ...
            <ScalarProperty Name="DNAME" ColumnName="DNAME" />
    ...

The usage example:

using(TestEntities4 db = new TestEntities4()) {
  var q = from e in db.Emp where e.DNAME == "RESEARCH" select e;
}


In EF4 you can define a partial class of the same name as your Entity Framework class in the same namespace and add read-only properties to that. I've just done that to expose the description of a connected Entity object as a read-only property of the original object.

namespace same.as.the.data.model
{
    public partial class Order
    {
        public string CustomerName
        {
            get { return Customer.Name; }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消