I've begun experimenting with LINQ to SQL and what I am doing is basically creating classes with LINQ mapping decorators - thereby choosing which parts of the db table schema I want to incorporate into my classes.
我已经开始尝试使用LINQ to SQL,我正在做的基本上是使用LINQ映射装饰器创建类 - 从而选择我想要将db表模式的哪些部分合并到我的类中。
A simple example:
一个简单的例子:
private DateTime? _LocalCopyTimestamp = (DateTime)SqlDateTime.MinValue;
[Column(Name = "recaLocalCopyTimestamp", Storage = "_LocalCopyTimestamp", CanBeNull = true)]
public DateTime? LocalCopyTimestamp
{
get
{
return this._LocalCopyTimestamp;
}
set
{
this._LocalCopyTimestamp = value;
}
}
I am not using and am not willing to resort to modeling tools due to project contraints (the way schema changes are handled and because there is an existing database schema and it is a bit too organic and non-strict)
我没有使用并且不愿意使用建模工具,因为项目约束(处理架构更改的方式以及因为存在现有的数据库架构而且它有点过于有机和非严格)
Is there a way to have this flexibility with the Entity Framework without having to include schema information files and/or lots of distinct code files?
有没有办法在Entity Framework中具有这种灵活性,而不必包含架构信息文件和/或许多不同的代码文件?
Could I then also create classes that "use" more than one underlying table?
那么我可以创建“使用”多个基础表的类吗?
Can anyone point me to documentation regarding this?
谁能指点我这方面的文件?
2 个解决方案
#1
The feature you are requesting (write C# classes and generate your model from those) is termed by the Entity Framework team "Model First." It does not exist in the current, shipping version of the Entity Framework, but is a planned feature for the next version. If you watch the Entity Framework talks from PDC, you can see demonstrations of this new feature. With the current version, you do not have to write "many" mapping files, but you do need one (the EDMX file), and it must be XML.
您要求的功能(编写C#类并从中生成模型)由实体框架团队称为“模型优先”。它不存在于实体框架的当前发布版本中,但是是下一版本的计划功能。如果您观看PDC的实体框架会话,您可以看到此新功能的演示。使用当前版本,您不必编写“许多”映射文件,但您确实需要一个(EDMX文件),它必须是XML。
Yes, you can create entity classes which use more than one underlying table. This is called "Entity splitting." Step-by-step instructions at the link. In general, you will find that the Entity Framework supports many more complicated mapping scenarios than LINQ to SQL.
是的,您可以创建使用多个基础表的实体类。这称为“实体分裂”。链接上的分步说明。通常,您会发现实体框架支持比LINQ to SQL更复杂的映射方案。
I'm afraid that I have to completely disagree with Marc regarding writing EDMX without use of the designer. Writing EDMX without using the designer is not only possible, but for projects exceeding a certain side, it is all but inevitable. A few points on this:
我担心我必须完全不同意Marc关于在不使用设计师的情况下编写EDMX的问题。不使用设计师编写EDMX不仅是可能的,而且对于超出某一方面的项目,它几乎是不可避免的。关于这一点的几点:
- For most of the early history (pre-RTM; "ObjectSpaces") of the Entity Framework, writing the XML files manually was the only way to use the tool. The designer is a recent feature, and is considerably less stable than the Entity Framework itself.
- There are certain Entity Framework features, such as complex types, which are not supported in the designer at all.
- Certain mapping scenarios, such as not mapping individual columns, or mapping tables without a foreign key relationship, which may be necessary for legacy databases, are not supported in the designer.
- As I mentioned in (1) the designer is quite a bit buggier than the Entity Framework itself. So on larger projects you will probably end up having to clean up after the designer's mistakes.
对于实体框架的大多数早期历史(RTM之前;“ObjectSpaces”),手动编写XML文件是使用该工具的唯一方法。设计器是最近的一个特性,并且比实体框架本身稳定得多。
某些Entity Framework功能(例如复杂类型)在设计器中根本不受支持。
设计器不支持某些映射方案,例如不映射单个列,或者没有外键关系的映射表,这可能是遗留数据库所必需的。
正如我在(1)中提到的那样,设计师比实体框架本身有点笨拙。因此,在较大的项目中,您可能最终必须在设计师的错误之后进行清理。
#2
Entity Framework uses the EDM to model data; this is a set of 3 complex schema files (storage, conceptual, mapping), most commonly stored as resources in the project (via the designer which uses a single EDMX file to generate all 3 schema files).
实体框架使用EDM对数据建模;这是一组3个复杂的模式文件(存储,概念,映射),最常见的存储为项目中的资源(通过设计器使用单个EDMX文件生成所有3个模式文件)。
It doesn't support attributed classes for this information. The only sensible way to write EDM is via the designer (essentially, a modelling tool which you dislike).
它不支持此信息的属性类。编写EDM的唯一合理方法是通过设计器(实际上是您不喜欢的建模工具)。
Re classes the "use" more than one underlying table; yes, a single Entity Framework entity at the conceptual layer (i.e. classes) can span multiple storage tables. This is especially useful for some inheritance examples, but can (IIRC) be used by flat models too. You do this via the "mappings" between the storage and conceptual layers (most commonly; on the tab in the designer).
重新命名“使用”多个基础表;是的,概念层(即类)的单个实体框架实体可以跨越多个存储表。这对于某些继承示例特别有用,但是平面模型也可以使用(IIRC)。您可以通过存储层和概念层之间的“映射”(最常见的是,在设计器的选项卡上)执行此操作。
#1
The feature you are requesting (write C# classes and generate your model from those) is termed by the Entity Framework team "Model First." It does not exist in the current, shipping version of the Entity Framework, but is a planned feature for the next version. If you watch the Entity Framework talks from PDC, you can see demonstrations of this new feature. With the current version, you do not have to write "many" mapping files, but you do need one (the EDMX file), and it must be XML.
您要求的功能(编写C#类并从中生成模型)由实体框架团队称为“模型优先”。它不存在于实体框架的当前发布版本中,但是是下一版本的计划功能。如果您观看PDC的实体框架会话,您可以看到此新功能的演示。使用当前版本,您不必编写“许多”映射文件,但您确实需要一个(EDMX文件),它必须是XML。
Yes, you can create entity classes which use more than one underlying table. This is called "Entity splitting." Step-by-step instructions at the link. In general, you will find that the Entity Framework supports many more complicated mapping scenarios than LINQ to SQL.
是的,您可以创建使用多个基础表的实体类。这称为“实体分裂”。链接上的分步说明。通常,您会发现实体框架支持比LINQ to SQL更复杂的映射方案。
I'm afraid that I have to completely disagree with Marc regarding writing EDMX without use of the designer. Writing EDMX without using the designer is not only possible, but for projects exceeding a certain side, it is all but inevitable. A few points on this:
我担心我必须完全不同意Marc关于在不使用设计师的情况下编写EDMX的问题。不使用设计师编写EDMX不仅是可能的,而且对于超出某一方面的项目,它几乎是不可避免的。关于这一点的几点:
- For most of the early history (pre-RTM; "ObjectSpaces") of the Entity Framework, writing the XML files manually was the only way to use the tool. The designer is a recent feature, and is considerably less stable than the Entity Framework itself.
- There are certain Entity Framework features, such as complex types, which are not supported in the designer at all.
- Certain mapping scenarios, such as not mapping individual columns, or mapping tables without a foreign key relationship, which may be necessary for legacy databases, are not supported in the designer.
- As I mentioned in (1) the designer is quite a bit buggier than the Entity Framework itself. So on larger projects you will probably end up having to clean up after the designer's mistakes.
对于实体框架的大多数早期历史(RTM之前;“ObjectSpaces”),手动编写XML文件是使用该工具的唯一方法。设计器是最近的一个特性,并且比实体框架本身稳定得多。
某些Entity Framework功能(例如复杂类型)在设计器中根本不受支持。
设计器不支持某些映射方案,例如不映射单个列,或者没有外键关系的映射表,这可能是遗留数据库所必需的。
正如我在(1)中提到的那样,设计师比实体框架本身有点笨拙。因此,在较大的项目中,您可能最终必须在设计师的错误之后进行清理。
#2
Entity Framework uses the EDM to model data; this is a set of 3 complex schema files (storage, conceptual, mapping), most commonly stored as resources in the project (via the designer which uses a single EDMX file to generate all 3 schema files).
实体框架使用EDM对数据建模;这是一组3个复杂的模式文件(存储,概念,映射),最常见的存储为项目中的资源(通过设计器使用单个EDMX文件生成所有3个模式文件)。
It doesn't support attributed classes for this information. The only sensible way to write EDM is via the designer (essentially, a modelling tool which you dislike).
它不支持此信息的属性类。编写EDM的唯一合理方法是通过设计器(实际上是您不喜欢的建模工具)。
Re classes the "use" more than one underlying table; yes, a single Entity Framework entity at the conceptual layer (i.e. classes) can span multiple storage tables. This is especially useful for some inheritance examples, but can (IIRC) be used by flat models too. You do this via the "mappings" between the storage and conceptual layers (most commonly; on the tab in the designer).
重新命名“使用”多个基础表;是的,概念层(即类)的单个实体框架实体可以跨越多个存储表。这对于某些继承示例特别有用,但是平面模型也可以使用(IIRC)。您可以通过存储层和概念层之间的“映射”(最常见的是,在设计器的选项卡上)执行此操作。