Adr*_*ore 8 entity-framework linq-to-sql
我正在将现有的应用程序从Linq移植到SQL到Entity Framework 4(默认代码生成).
我注意到两者之间的一个区别是重置对象引用时不会更新外键属性.现在我需要决定如何处理这个问题.
例如,假设您有两个实体类型,Company和Employee.一家公司有很多员工.
在Linq To SQL中,设置公司还设置公司ID:
var company=new Company(ID=1);
var employee=new Employee();
Debug.Assert(employee.CompanyID==0);
employee.Company=company;
Debug.Assert(employee.CompanyID==1); //Works fine!
Run Code Online (Sandbox Code Playgroud)
在实体框架中(并且不使用任何代码模板自定义),这不起作用:
var company=new Company(ID=1);
var employee=new Employee();
Debug.Assert(employee.CompanyID==0);
employee.Company=company;
Debug.Assert(employee.CompanyID==1); //Throws, since CompanyID was not updated!
Run Code Online (Sandbox Code Playgroud)
如何使EF的行为与LinqToSQL相同?我看了默认代码生成T4模板,但我无法弄清楚如何进行必要的更改.看起来像一个单行应该可以做到这一点,但我无法弄清楚如何获得给定引用的ID属性.
从我在默认 T4 模板中看到的情况来看,实体的外键属性并不直接链接到与该键关联的实体引用。
有几种方法可以解决您有关从 Linq 迁移到 SQL 再到 EF4 的问题。其中之一是注册AssociationChanged您协会的活动,以便它自动更新您的字段。在您的情况下,一种方法可能是这样的:
// Extends Employee entity
public partial class Employee
{
private void CompanyChanged(Object sender, CollectionChangeEventArgs e)
{
// Apply reactive changes; aka set CompanyID
// here
}
// Create a default constructor that registers your event handler
public Employee()
{
this.CompanyReference.AssociationChanged += CompanyChanged;
}
}
Run Code Online (Sandbox Code Playgroud)
就个人而言,如果您想限制维护此类逻辑所需的维护,我建议更改您的 T4 模板(您自己更改或找到一个),以便它设置更改时间,CompanyId如Company前所示。
Gil Fink写了一篇关于 EF4 的 T4 模板的非常好的介绍,您可以查找Scott Hanselman提供的大量有用的链接和资源,以使用 T4 模板。
最后一点,除非我弄错了,否则直接访问外键作为实体的属性是 EF3.5 到 4 中的新内容。在 3.5 中,访问它的唯一方法是通过关联实体 ( Employee.Company.CompanyID)。我相信 EF4 中添加了该功能,因此您在从数据存储中进行选择时无需加载关联(使用“include”)即可获取外键。
也许 EF 对此的看法是,如果你有关联,首先通过关联获取 ID。但这只是猜测,因为我没有得到任何报价来支持它。
[编辑 2010-06-16]:在快速通读和分析 edmx xml 元素后,我发现了一个名为 ReferentialConstraint 的元素,它似乎包含特定 FK_Relation 的外键字段。
下面是在默认 T4 edmx 模板内修改的代码片段,写入导航属性部分。( Template_RegionNavigationProperties),位于未修改模板的第 388 行附近。尝试忽略可怕的格式......
<#=code.SpaceAfter(NewModifier(navProperty))#><#=Accessibility.ForProperty(navProperty)#> <#=MultiSchemaEscape(navProperty.ToEndMember.GetEntityType(), code)#> <#=code.Escape(navProperty)#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(navProperty))#>get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<<#=MultiSchemaEscape(navProperty.ToEndMember.GetEntityType(), code)#>>("<#=navProperty.RelationshipType.FullName#>", "<#=navProperty.ToEndMember.Name#>").Value;
}
<#=code.SpaceAfter(Accessibility.ForSetter(navProperty))#>set
{
// edit begins here
if(value != null)
{
// Automatically sets the foreign key attributes according to linked entity
<#
AssociationType association = GetSourceSchemaTypes<AssociationType>().FirstOrDefault(_ => _.FullName == navProperty.RelationshipType.FullName);
foreach(var cons in association.ReferentialConstraints)
{
foreach(var metadataProperty in cons.FromProperties)
{
#>
this.<#=metadataProperty.Name#> = value.<#=metadataProperty.Name#>;
//this._<#=metadataProperty.Name#> = value._<#=metadataProperty.Name#>; // use private field to bypass the OnChanged events, property validation and the likes..
<#
}
}
#>
}
else
{
// what usually happens in Linq-to-SQL when an association is set to null
// here
}
// edit ends here
((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<<#=MultiSchemaEscape(navProperty.ToEndMember.GetEntityType(), code)#>>("<#=navProperty.RelationshipType.FullName#>", "<#=navProperty.ToEndMember.Name#>").Value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
我粗略地测试了它,但它是一个假设,有一些验证和这样的缺失。无论如何,也许它可以为您提供解决方案的提示。
| 归档时间: |
|
| 查看次数: |
2251 次 |
| 最近记录: |