EF6和EF4.1在文件层次结构中的区别 [英] The difference between EF6 and EF4.1 in files hierarchy
问题描述
我是实体框架的初学者。
我注意到当我使用Visual Studio 2013 EF6时, >:
我有 .Designer.cs
空文件与此评论:
//为C:\Users\Luka\Desktop\Test\EF- db2008\AdventureWorks\启用了T4代码生成AdventureWorksLib\AdventureWorksLib\AWLTModel.edmx。
//要启用旧代码生成,请将代码生成策略设计器
//属性的值更改为Legacy ObjectContext。当模型
//在设计器中打开时,该属性可在属性窗口中使用。
//如果没有生成上下文和实体类,可能是因为您创建了一个空模型,但
//尚未选择要使用哪个版本的Entity Framework。要为您的模型生成上下文类和实体
//类,请在设计器中打开模型,右键单击设计器表面,
//选择从数据库更新模型... ,从模型生成数据库,或添加代码生成
//项目...。
.Context.tt
及其 .Context.cs
,代码如下:
public partial class AWLTEntities:DbContext
{
public AWLTEntities()
:base(name = AWLTEntities)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet< Address>地址{get;组; }
public virtual DbSet< Customer>客户{get;组; }
}
然后 .tt
文件与 .cs
文件为每个实体,如 Customer.cs
使用如下代码:
public partial class Customer
{
public Customer )
{
this.NameStyle = false;
this.CustomerAddresses = new HashSet< CustomerAddress>();
this.Orders = new HashSet< Order>();
}
public int CustomerID {get;组; }
}
不同于当我使用 EF4.1与visual studio 2010
,
文件只有一个代码 .Designer.cs
为模型!!
- 有些人可以帮助我了解什么是这些文件为
.Context.tt
,.Context.cs
,。在两种情况下,文件层次结构中的
?
与code>,.cs
EF4.1) - 我找不到
OnPropertyChanging(Value)& OnPropertyChanged()
在
EF6
验证我的实体!!为什么这些方法不再存在,以及如何验证我的
属性如果不存在?
所以让我们一步一步的说明: / p>
-
您的
.edmx
文件是从设计人员或从现有数据库生成。但它只是一个xml文件,其中包含有关使用的数据库结构的信息 - 。他们都将在.Designer.cs
文件中。但是这不是建议的(但是您仍然可以获得相应的T4模板 - 以这种方式,您将得到OnPropertyChanged
和OnPropertyChanging
,因为它们是EntityObect
类,但它们受到保护,因此您可能需要编写一些包装器)。但是最好使用 POCO类和DbContext模板 - 一个VS 2013在你的情况下使用。然后,您将得到单独的.Context.tt
以生成.Context.cs
与派生DbContext
其中包含DbSets
表示表,并且.tt
文件以生成实体类。而.tt
和.cs
之间的层次结构只显示哪个.cs
由.tt
生成,而只有.cs
将在您的应用程序运行时实际执行并执行
- 现在关于
OnPropertyChanged
- 这应该只是一个INotifyPropertyChanged
界面。但是,您正在使用生成POCO类的模板。这是默认和推荐的选项,但为了实现INotifyPropertyChanged
,您可能需要编辑模板或从Visual Studio在线图库中选择另一个模板。然而,这可能不是最佳的架构解决方案,因为有时更好地将实体和用于UI /业务逻辑的类分开。
I'm a beginner to Entity Framework .
I notice that When I use
EF6 with Visual studio 2013
:I have
.Designer.cs
empty file with this comment:// T4 code generation is enabled for model 'C:\Users\Luka\Desktop\Test\EF-db2008\AdventureWorks\AdventureWorksLib\AdventureWorksLib\AWLTModel.edmx'. // To enable legacy code generation, change the value of the 'Code Generation Strategy' designer // property to 'Legacy ObjectContext'. This property is available in the Properties Window when the model // is open in the designer. // If no context and entity classes have been generated, it may be because you created an empty model but // have not yet chosen which version of Entity Framework to use. To generate a context class and entity // classes for your model, open the model in the designer, right-click on the designer surface, and // select 'Update Model from Database...', 'Generate Database from Model...', or 'Add Code Generation // Item...'.
.Context.tt
and its.Context.cs
with code like this:public partial class AWLTEntities : DbContext { public AWLTEntities() : base("name=AWLTEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Address> Addresses { get; set; } public virtual DbSet<Customer> Customers { get; set; } }
And then
.tt
file with.cs
file for each entity likeCustomer.cs
With code like this :
public partial class Customer { public Customer() { this.NameStyle = false; this.CustomerAddresses = new HashSet<CustomerAddress>(); this.Orders = new HashSet<Order>(); } public int CustomerID { get; set; } }
This 's totally different when i use
EF4.1 with visual studio 2010
, there's only one code behind file.Designer.cs
for the model !!
- Could some one help me to understand what are all these files for
.Context.tt
,.Context.cs
,.tt
,.cs
?and what 's different in files hierarchy between the two cases(EF6,EF4.1)
? - I can't find
OnPropertyChanging(Value) & OnPropertyChanged()
inEF6
to validate my entities !!Why these methods no longer exist and how to validate my properties if they do not exist?
解决方案So lets make it clear step by step:
You've got your
.edmx
file, which was created from designer or generated from existing database. But it's only an xml file, which contains info about the database structure that is used - storage scheme, info about entities - conceptual schema and mappings between those two. It doesn't contain any executable code. This code needs to be generated.To generate the code the
.edmx
file will be parsed and.cs
files will be created that contain actual executable code. 2 approaches might be used:Generator build-in the Visual Studio - the EntityModelCodeGenerator tool. That is a legacy approach, that was used previously (in Visaul Studio 2010 in your case). This will generate only the
.Designer.cs
file with all classes inside it. But this approach is not the best - you cannot modify generation process for your needs (say, addDataMember
attribute to generated classes or some other changes). That's why it's better to useT4 templates. These are files with
.tt
extension. All they do is just execute their logic whenRun custom tool
is chosen in context menu, or.edmx
file is changed. There is a set of available templates for generating EF code from.edmx
, some info here. And because these are just regular files, you could modify them as you need (to get better editor experience use tangible T4 extension). Some basic info about the usage of T4 templates in EF here.
You can choose between these 2 approaches independently of Visual Studio version - just change the
Code generation strategy
property in the properties of your.edmx
file:If the
Legacy ObjectContext
option is chosen - you get 1-st way with single.Designer.cs
file. IfT4
- then the.Designer.cs
will be empty (with comments saying that T4 templates are used) and.tt
files will generate the used code. So if you need the same code as in VS 2010 - just useLegacy ObjectContext
option.Another difference between those two is that 1-st generates legacy
ObjectContext
and entities that are derived from EntityObect. They all will be in that.Designer.cs
file. But that is not recommended any more (however you can still get corresponding T4 template - in that way you'll get yourOnPropertyChanged
andOnPropertyChanging
back, because they are the methods ofEntityObect
class, however they are protected, so you might need to write some wrappers). But its better to use POCO classes and DbContext template - the one that VS 2013 used in your case. Then you'll get separate.Context.tt
to generate.Context.cs
with derivedDbContext
in it withDbSets
representing tables, and.tt
file to generate the entity classes. And the hierarchy between.tt
and.cs
only shows which.cs
were generated by which.tt
, while only.cs
will be actually complied and executed when your app runs.- And now regarding
OnPropertyChanged
- that should be just an implementation ofINotifyPropertyChanged
interface. However looks like you are using template that generate POCO classes. That is the default and recommended option, but to get the implementation ofINotifyPropertyChanged
you might need to edit the template or choose another one from Visual Studio Online gallery. That's, however, might not be the best architectural solution, because it is sometimes better to separate entities and the classes you use for UI/Business logic.
这篇关于EF6和EF4.1在文件层次结构中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 现在关于