How can I access other then the “General” attributes of a Entity-Property from a EntityModel within a T4?

为君一笑 提交于 2019-12-11 05:04:50

问题


I am using sort of the following code to get all properties of a entity

IList<EdmProperty> list = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity)

Then I iterate through these list, access each Property and read the Property Properties (Yeah, much properties, hope no one gets to confused).

While I can easily access the General attributes I don't know how to access the other Properties of the the Entity-Property like Max Length & Fixed Length


回答1:


Those properties are not part of PrimitiveType. They are directly in p.TypeUsage under Facets property.




回答2:


Try the following code:

var MaxLength = (property as EdmMember).TypeUsage.Facets.Where(f => f.Name == "MaxLength").SingleOrDefault();
int maxLength = -1;
if(MaxLength != null)
  maxLength = (int)MaxLength.Value;  

You can use the maxLength variable in the template code. Any other facet can be accessed in a similar way.




回答3:


  protected void RecognizeByMetadata(IList<Facet> facets)
    {
        //Dictionary<string, string> attributes = new Dictionary<string, string>();
        //facets.AsParallel().ForAll(x => attributes.Add(x.Name, x.Value + ""));
        try{
        var t = facets.Where(x => x.Name == "MaxLength").Select(x => x.Value).FirstOrDefault();

        if (t != null)
        {

            string typ = t.GetType().FullName;

            this.isMax = (t.ToString() == "Max");

            if (!isMax)
                this.MaxLength = (int?)t;
        }
        else
        {
            isMax = false;
            MaxLength = null;
        }

        this.IsNullable = (bool?)facets.Where(x => x.Name == "Nullable").Select(x => x.Value).FirstOrDefault();
        this.Defaultvalue = facets.Where(x => x.Name == "DefaultValue").Select(x => x.Value).FirstOrDefault();
        this.IsUnicode = (bool?)facets.Where(x => x.Name == "Unicode").Select(x => x.Value).FirstOrDefault();
        this.IsFixedlength = (bool?)facets.Where(x => x.Name == "FixedLength").Select(x => x.Value).FirstOrDefault();
        //string precision    = facets.Where(x => x.Name == "Precision").Select(x => x.Value + "").FirstOrDefault();
        //string scale        = facets.Where(x => x.Name == "Scale").Select(x => x.Value + "").FirstOrDefault();
        isRecognized = true;
        recognizeUnique();
        } catch (Exception e)
        {
        string mewssage = e.Message;
        throw;
        }
    }


来源:https://stackoverflow.com/questions/6620645/how-can-i-access-other-then-the-general-attributes-of-a-entity-property-from-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!