How can I access other then the “General” attribut

2019-08-10 20:36发布

问题:

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