zoukankan      html  css  js  c++  java
  • EF Power Tools的Reverse Engineer Code First逆向生成Model时处理计算字段

    VS2013上使用EF Power Tools的Reverse Engineer Code First逆向生成Model时,没有处理计算字段。在保存实体时会出现错误。

    可以通过修改Mapping.tt解决。

    打开Mapping.tt,找到

    foreach (var prop in efHost.EntityType.Properties)
        {
            var type = (PrimitiveType)prop.TypeUsage.EdmType;
            var isKey = efHost.EntityType.KeyMembers.Contains(prop);
            var storeProp = efHost.PropertyToColumnMappings[prop];
            var sgpFacet = storeProp.TypeUsage.Facets.SingleOrDefault(f => f.Name == "StoreGeneratedPattern");
            var storeGeneratedPattern = sgpFacet == null
                ? StoreGeneratedPattern.None
                : (StoreGeneratedPattern)sgpFacet.Value;
                
            var configLines = new List<string>();
                 
            if (type.ClrEquivalentType == typeof(int)
                || type.ClrEquivalentType == typeof(decimal)
                || type.ClrEquivalentType == typeof(short)
                || type.ClrEquivalentType == typeof(long))
            {
                if (isKey && storeGeneratedPattern != StoreGeneratedPattern.Identity)
                {
                    configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)");
                }
                else if ((!isKey || efHost.EntityType.KeyMembers.Count > 1) && storeGeneratedPattern == StoreGeneratedPattern.Identity)
                {
                    configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
                }
            }

    在 var configLines = new List<string>(); 后添加计算字段的内容:

    foreach (var prop in efHost.EntityType.Properties)
        {
            var type = (PrimitiveType)prop.TypeUsage.EdmType;
            var isKey = efHost.EntityType.KeyMembers.Contains(prop);
            var storeProp = efHost.PropertyToColumnMappings[prop];
            var sgpFacet = storeProp.TypeUsage.Facets.SingleOrDefault(f => f.Name == "StoreGeneratedPattern");
            var storeGeneratedPattern = sgpFacet == null
                ? StoreGeneratedPattern.None
                : (StoreGeneratedPattern)sgpFacet.Value;
                
            var configLines = new List<string>();
    
            if(!isKey && storeGeneratedPattern == StoreGeneratedPattern.Computed)
            {
                configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)");
            }
                 
            if (type.ClrEquivalentType == typeof(int)
                || type.ClrEquivalentType == typeof(decimal)
                || type.ClrEquivalentType == typeof(short)
                || type.ClrEquivalentType == typeof(long))
            {
                if (isKey && storeGeneratedPattern != StoreGeneratedPattern.Identity)
                {
                    configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)");
                }
                else if ((!isKey || efHost.EntityType.KeyMembers.Count > 1) && storeGeneratedPattern == StoreGeneratedPattern.Identity)
                {
                    configLines.Add(".HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)");
                }
            }

    再次使用Reverse Engineer Code First。

    得到带计算字段信息的mapping。

                this.Property(t => t.ResetDate)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
    
                this.Property(t => t.ResetHour)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
  • 相关阅读:
    MVC4学习-View(0)
    javascript与常用正则表达式
    uhfreader&rfid标签测试
    WebClient文件上传很方便哈
    NAudio的简单用法
    与wmi交互,调非托管代码,单元测试遇到的一些问题
    我在这里骑美团单车被交警罚了50元,这个地方不能骑共享单车大家留意了
    发邮件,美化table格式
    学习jwt的简单使用
    学习redis的基本使用
  • 原文地址:https://www.cnblogs.com/Gun/p/5014840.html
Copyright © 2011-2022 走看看