EF 6 数据库首先将生成的实体中的所有属性设为虚拟 类
EF 6 database first make all properties virtual in generated entity classes
我有一个数据库优先的 edmx 模型。它生成的部分 类 具有非虚拟简单属性。例如
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------
public partial class Entity
{
public int Id {get;set;} //not virtual
public string SomeOtherProperty {get;set;} //also not virtual
public virtual ICollection<RelatedEntity> RelatedCollection {get;set;} //but 'navigational' properties are virtual.
}
如何告诉设计者将所有属性都虚拟化?
有一个简单的解决方案。
在文件里面,找到CodeStringGenerator
class,查找这个方法:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
一个简单的编辑就足够了:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
//make properties virtual.
AccessibilityAndVirtual(Accessibility.ForProperty(edmProperty)),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
仅此而已,供后人参考。
我有一个数据库优先的 edmx 模型。它生成的部分 类 具有非虚拟简单属性。例如
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------
public partial class Entity
{
public int Id {get;set;} //not virtual
public string SomeOtherProperty {get;set;} //also not virtual
public virtual ICollection<RelatedEntity> RelatedCollection {get;set;} //but 'navigational' properties are virtual.
}
如何告诉设计者将所有属性都虚拟化?
有一个简单的解决方案。
在文件里面,找到CodeStringGenerator
class,查找这个方法:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
一个简单的编辑就足够了:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}get; {4}set; }}",
//make properties virtual.
AccessibilityAndVirtual(Accessibility.ForProperty(edmProperty)),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
仅此而已,供后人参考。