除了“常规”之外,我如何访问其他内容? T4 中 EntityModel 的 Entity-Property 的属性?

发布于 2024-11-19 04:41:40 字数 353 浏览 5 评论 0原文

我使用以下代码来获取实体的所有属性

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

然后我迭代这些列表,访问每个属性并读取属性属性(是的,很多属性,希望没有人感到困惑)。

虽然我可以轻松访问 General 属性,但我不知道如何访问实体属性的其他属性,例如 Max Length & 固定长度

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谢绝鈎搭 2024-11-26 04:41:40

这些属性不是 PrimitiveType 的一部分。它们直接位于Facets 属性下的p.TypeUsage 中。

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

一身仙ぐ女味 2024-11-26 04:41:40

尝试以下代码:

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

您可以在模板代码中使用 maxLength 变量。可以以类似的方式访问任何其他方面。

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.

烟雨扶苏 2024-11-26 04:41:40
  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;
        }
    }
  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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文