实体框架:我在哪里扩展CSDL / MSL? [英] Entity Framework: Where do I extend the CSDL/MSL?

查看:145
本文介绍了实体框架:我在哪里扩展CSDL / MSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4.我正在使用数据库第一个模型,这意味着我从数据库生成了EDM。现在我想添加一些模型定义的函数。我的问题是...在哪里?



如果我把它们放在.edmx文件中,下次我更新数据库并生成新的EDM?我的意思是说它位于.Designer.cs文件的顶部,如果重新生成代码,则该文件的手动更改将被覆盖。



所以在什么文件中我添加了什么?

解决方案

我将会稍微深入一点,因为模型定义的函数不是很好已知的。



模型定义的函数必须手动添加到EDMX文件的CSDL部分。您必须以XML格式打开文件并添加一个功能。例如,该模型定义的函数能够生成员工的全名:

 < Function Name =FullNameReturnType = Edm.String > 
< Parameter Name =empType =TestModel.Employee/>
< DefiningExpression>
Trim(emp.FirstName)++ Trim(emp.LastName)
< / DefiningExpression>
< / Function>

现在您可以保存EDMX并返回设计师。该功能仍然存在,但在模型浏览器中不可见。您可以从数据库更新模型或删除所有实体,但该函数仍将被定义。 EF不会删除EDMX的CSDL部分的自定义修改。



现在,您需要定义.NET函数才能使用此模型定义的函数。你可以在任何地方做一种方法是使用部分类上下文,但在同一时间你可以使用一些cutom类:

  public static class EdmFunctions 
{
[EdmFunction(TestModel,FullName)]
public static string FullName(Employee e)
{
throw new NotSupportedException(仅用于L2E查询。);
}
}

你完成了唯一剩下的任务是使用Linq-to-entities查询中的函数:

  using(var context = new TestEntities()) 
{
var query = from e in context.Employees
select new
{
e.Id,
FullName = EdmFunctions.FullName(e)
};
var data = query.ToList();
...
}

模型定义的函数只是一些可重用的实体SQL它被转换为SQL,因此它们只能用于Linq到实体的查询。模型定义的函数可以复杂得多。


I'm using Entity Framework 4. I am using a database first model, meaning that I generated the EDM from the database. Now I want to add some model-defined functions. My question is ... where?

If I put them in the .edmx file, won't all my additions be clobbered the next time I update the database and generate the new EDM? I mean it says it right there at the top of the .Designer.cs file, "Manual changes to this file will be overwritten if the code is regenerated."

So, in what file do I put my additions?

解决方案

I will take it little bit deeply because model defined functions are not very well known.

Model defined functions must be manually added to CSDL part of EDMX file. You must open file as XML and add a function. For example this model defined function is able to produce full name of the employee:

<Function Name="FullName" ReturnType="Edm.String">
  <Parameter Name="emp" Type="TestModel.Employee" />
  <DefiningExpression>
    Trim(emp.FirstName) + " " + Trim(emp.LastName)
  </DefiningExpression>
</Function>

Now you can save your EDMX and return to designer. The function will be still present but it is not visible in Model browser. You can update your model from database or delete all your entities but the function will be still defined. EF doesn't remove custom modification in CSDL part of EDMX.

Now you need to define the .NET function to be able to use this model defined function. You can do it anywhere. One way is to use partial class to context but in the same time you can just use some cutom class:

public static class EdmFunctions
{
    [EdmFunction("TestModel", "FullName")]
    public static string FullName(Employee e)
    {
        throw new NotSupportedException("This function is only for L2E query.");
    }
}

And you are done. The only remaining task is using the function in Linq-to-entities query:

using (var context = new TestEntities())
{
    var query = from e in context.Employees
                select new
                    {
                        e.Id,
                        FullName = EdmFunctions.FullName(e)
                    };
    var data = query.ToList();
    ...
}

Model defined functions are just some reusable Entity SQL which is translated to SQL so they can be only used in Linq-to-entities queries. Model defined functions can be much more complicated.

这篇关于实体框架:我在哪里扩展CSDL / MSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆