简体   繁体   English

EF 6数据库首先使所有属性在生成的实体类中虚拟化

[英]EF 6 database first make all properties virtual in generated entity classes

I have a db first edmx model. 我有一个db第一个edmx模型。 The partial classes it generates have non-virtual simple properties. 它生成的部分类具有非虚拟简单属性。 Eg 例如

//------------------------------------------------------------------------------
// <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.
    }

How to tell the designer to make all properties virtual? 如何告诉设计师将所有属性虚拟化?

There is a simple solution. 一个简单的解决方案。

展开模型edmx并编辑Model.tt

Inside the file, find the CodeStringGenerator class, lookup this method: 在文件内部,找到CodeStringGenerator类,查找此方法:

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)));
}

One simple edit will be enough: 一个简单的编辑就足够了:

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)));
}

And that's it, for reference of future generations. 就是这样,供后代参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 EF 生成的实体上的所有属性都是虚拟的? - How can I make all properties virtual on entities generated by EF? EF代码优先重命名生成的POCO类中的属性 - EF Code First renaming of properties in generated POCO classes EF代码优先和虚拟属性 - EF code first and virtual properties 从“代码优先自数据库” EF6更改生成的类 - Changing the generated classes from “Code First From Database” EF6 为何EF急切地首先使用EF数据库加载所有导航属性? - Why is EF eagerly loading all navigation properties with EF Database first? 首先在metaDataClass中为由EF数据库生成的实体添加注释[key] - Adding annotation [key] in metaDataClass for an entity generated by EF database first 更改实体框架数据库首先自动生成的域类 - Change entity framework database first auto generated domain classes 首先在EF数据库中自定义属性 - Custom properties in EF database first 在使用 Moq 和 xUnit 的单元测试中使 mocking 的所有 EF Core model 属性虚拟化? - Make all EF Core model properties virtual for mocking in unit tests with Moq and xUnit? 如何在我的DAL内部创建我的EF代码优先类的某些属性? - How can I make certain properties of my EF code-first classes internal to my DAL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM