-->

EF 5型号阶偏类自定义构造函数如何?(EF 5 Model First Partial Class

2019-07-18 14:25发布

英孚已经对我产生了一些局部类,每一个构造函数,但它说不要去碰它们(如下图所示),现在如果让我自己中学部分班级,我想有一个构造函数,自动设置某些字段如何我这样做,因为它会冲突?

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

namespace Breakdown.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Call
    {
        public Call()
        {
            this.Logs = new HashSet<Log>();
        }

        ...
    }
}

Answer 1:

分部方法可以帮助你在这里,在T4模板定义体少部分方法和调用构造函数里面。

public <#=code.Escape(entity)#>()
{
    ...
    OnInit();
}

partial void OnInit();

然后在您的局部类定义分部方法和地点内要在构造函数中做什么。 如果你不想做任何事情,那么你就需要定义分部方法。

partial class Entity()
{
    partial void OnInit()
    {
        //constructor stuff
        ...
    }
}

http://msdn.microsoft.com/en-us/library/vstudio/6b0scde8.aspx



Answer 2:

这是不可能的。

部分课堂是指同一类的部分。

没有方法可以定义两次或覆盖(规则同样适用于构造也)

但是,您可以使用下面提及的解决方法:

//From file SomeClass.cs - generated by the tool
public partial class SomeClass
 {
    // ...
 }


// From file SomeClass.cs - created by me
public partial class SomeClass
  {
    // My new constructor - construct from SomeOtherType
    // Call the default ctor so important initialization can be done
    public SomeClass(SomeOtherType value) : this()
      {

       }
  } 

有关更多信息,请检查分部类,默认构造函数

我希望这会帮助你。



Answer 3:

我想最近做同样的,最终修改T4模板,以便我可以手动实现自己的参数的构造函数。 要做到这一点,你可以从生成的类中删除构造函数和移动收藏等的实例化的构造函数外所以这个:

public Call()
{
  this.Logs = new HashSet<Log>();
}

成为本:

private ICollection<Log> logs = new HashSet<Log>();
public virtual ICollection<Log> Logs 
{ 
  get { return this.logs; } 
  set { this.logs = value; } 

}

我想,缺点是生成的类是不作为“干净”。 也就是说你不能只为您的复杂/导航类型自动实现的属性。

在你model.tt文件,你可以通过删除下面的代码,注释掉它或者所以它从来没有得到执行只是把在一个虚假的入条件防止构造代:

if (propertiesWithDefaultValues.Any() || complexProperties.Any())
{
#>
  public <#=code.Escape(complex)#>()
  {
<#
    foreach (var edmProperty in propertiesWithDefaultValues)
    {
#>
      this.<#=code.Escape(edmProperty)#> =
         <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
<#
    }

    foreach (var complexProperty in complexProperties)
    {
#>
      this.<#=code.Escape(complexProperty)#> = new
        <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
    }
#>
  }

然后下面这一点,你需要做的,其属性为您的复杂和导航类型产生了一些修改。 添加与对象实例化一个私人var和访问私有VAR为这些如的属性:

if (complexProperties.Any())
{
  foreach(var complexProperty in complexProperties)
  {
    //generate private var + any instantiation
    //generate property for accessing var
  }
}

根据模型的复杂性有可能是你需要修改的其他领域。 希望这可以让你开始。



Answer 4:

如果我清楚地明白这个问题,你创建一个新的实体时,需要此构造方法,那就是没有坚持之前的实体。

我的情况是设置一个默认值的所有日期时间,也就是initalize他们“的时间开始时”:1900-01-01。

在这种情况下,我使用实体工厂

public static T GetNewEntity<T> () {
    T e;
    try {
        e = Activator.CreateInstance<T>();
    } catch {
        e = default(T);
    }
    SetDefaults(e);

    return e;
}

每当我需要一个新的实体我用

Entity e = GetNewEntity<Entity>();

与SetDefaults为:

public static void SetDefaults (object o) {
    Type T = o.GetType();
    foreach ( MemberInfo m in T.GetProperties() ) {
        PropertyInfo P = T.GetProperty(m.Name);
        switch ( Type.GetTypeCode(P.PropertyType) ) {
            case TypeCode.String :
                if ( P.GetValue(o, null) == null ) 
                    P.SetValue(o, String.Empty, null); 
                break;
            case TypeCode.DateTime :
                if ( (DateTime)P.GetValue(o, null) == DateTime.MinValue )
                    P.SetValue(o, EntityTools.dtDef, null); 
                break;
        }
    }
}

完整的代码是在这里

它可以被rewrittent考虑实体类型等等...



Answer 5:

  1. 添加一个基类:

      public class CallBase { protected CallBase() { Initialize(); } protected abstract void Initialize(); } 
  2. 添加局部类实现在另一个文件

      public partial class Call: CallBase { protected override void Initialize(); { ... } } 

缺点是初始化方法将所有收集的动物之前被调用。



文章来源: EF 5 Model First Partial Class Custom Constructor How To?