如何查看 ADO.NET 实体数据模型的代码
How to view the code of an ADO.NET Entity Data Model
我创建了一个 ADO.NET 实体数据模型。去哪里查看实际的class?我想检查数据类型并添加一些 DisplayName
属性。
这是解决方案资源管理器中的模型:
谢谢。
当您从数据库生成模型时(这里似乎就是这种情况),会创建一些不同的代码文件。要获取上下文的代码,请展开 ProgramMasterList.Context.tt
。您会在其中看到上下文 class.
的 .cs 文件
然后,对于您从数据库中选择成为模型一部分的每个 table,都会创建一个实体 class。您可以通过展开 ProgramMasterList.tt
找到它们。例如,如果您有一个名为 Person 的 table,您可能有一个实体 class 文件 Person.cs
,其中定义了 Person
class。
现在,您提到要修改 classes 以添加属性。如果你修改了ProgramMasterList.tt
下的class文件,这些文件是由Entity Framework动态生成的,那么下次你从数据库更新你的模型时(比如添加一个新的table 从您的数据库到您的模型),您所做的任何更改都将被覆盖。幸运的是,有更好的方法。 ProgramMasterList.tt
下的每个 class 都是 部分 class。因此,您可以添加到 class 而无需修改 Entity Framework 自动生成的文件。只需创建一个新文件,声明 Person class(部分),然后在其中添加额外的方法、属性等。我建议将所有这些 "extensions" 放入一个文件夹中以保持它们井井有条,但这取决于您。
因此,它可能看起来像这样:
解决方案结构:
- ProgramMasterList.edmx
- ProgramMasterList.Context.tt
- ProgramMasterList.Context.cs
- ProgramMasterList.Designer.cs
- ProgramMasterList.edmx.图表
- ProgramMasterList.tt
- Person.cs
- 扩展
- Person.cs
Person.cs(低于ProgramMasterList.tt)
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SomeNamespace
{
using System;
using System.Collections.Generic;
public partial class Person
{
public Person()
{
}
public int PersonID { get; set; }
public string Name { get; set; }
public DateTime Birthdate { get; set; }
}
}
Person.cs(在扩展文件夹中)
注意:确保命名空间与其他 Person.cs 文件中的命名空间完全匹配。
namespace SomeNamespace
{
public partial class Person
{
// Custom property (not auto-generated by Entity Framework)
public string DisplayName
{
get { return PersonID + " - " + Name + " (" + Birthdate.ToString() + ")"; }
}
}
}
我创建了一个 ADO.NET 实体数据模型。去哪里查看实际的class?我想检查数据类型并添加一些 DisplayName
属性。
这是解决方案资源管理器中的模型:
谢谢。
当您从数据库生成模型时(这里似乎就是这种情况),会创建一些不同的代码文件。要获取上下文的代码,请展开 ProgramMasterList.Context.tt
。您会在其中看到上下文 class.
然后,对于您从数据库中选择成为模型一部分的每个 table,都会创建一个实体 class。您可以通过展开 ProgramMasterList.tt
找到它们。例如,如果您有一个名为 Person 的 table,您可能有一个实体 class 文件 Person.cs
,其中定义了 Person
class。
现在,您提到要修改 classes 以添加属性。如果你修改了ProgramMasterList.tt
下的class文件,这些文件是由Entity Framework动态生成的,那么下次你从数据库更新你的模型时(比如添加一个新的table 从您的数据库到您的模型),您所做的任何更改都将被覆盖。幸运的是,有更好的方法。 ProgramMasterList.tt
下的每个 class 都是 部分 class。因此,您可以添加到 class 而无需修改 Entity Framework 自动生成的文件。只需创建一个新文件,声明 Person class(部分),然后在其中添加额外的方法、属性等。我建议将所有这些 "extensions" 放入一个文件夹中以保持它们井井有条,但这取决于您。
因此,它可能看起来像这样:
解决方案结构:
- ProgramMasterList.edmx
- ProgramMasterList.Context.tt
- ProgramMasterList.Context.cs
- ProgramMasterList.Designer.cs
- ProgramMasterList.edmx.图表
- ProgramMasterList.tt
- Person.cs
- ProgramMasterList.Context.tt
- 扩展
- Person.cs
Person.cs(低于ProgramMasterList.tt)
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SomeNamespace
{
using System;
using System.Collections.Generic;
public partial class Person
{
public Person()
{
}
public int PersonID { get; set; }
public string Name { get; set; }
public DateTime Birthdate { get; set; }
}
}
Person.cs(在扩展文件夹中) 注意:确保命名空间与其他 Person.cs 文件中的命名空间完全匹配。
namespace SomeNamespace
{
public partial class Person
{
// Custom property (not auto-generated by Entity Framework)
public string DisplayName
{
get { return PersonID + " - " + Name + " (" + Birthdate.ToString() + ")"; }
}
}
}