Skip to content

Not respecting MetadataType? #585

@markleavesley

Description

@markleavesley

I am trying to implement defining attributes for a class in another class using MetadataType as per MSDN (https://msdn.microsoft.com/en-gb/library/ff664465(v=pandp.50).aspx). I'm using XF 2.3.4.247 in VS 2017 with PCL profile 259.

As far as I can tell this is supported in .NET Standard 1.6 and above, but I need 1.4 for UWP. It is not supported in PCL/XF out the box. I found a nuget package that implements this for PCL (https://github.com/ryanhorath/PortableDataAnnotations).

Based on MSDN it would be like this:

namespace a_sln
{
  [MetadataType(typeof(fredmetadata))]
  public class fred
  {
    public int id {get; set;}
    public List<int> nums {get; set;}
  }
  
  [Table("tbl_fred")]
  public class fredmetadata
  {
    [PrimaryKey]
    public object id {get; set;}
    [Ignore]
    public object nums {get; set;}
  }
}

When I call CreateTableAsync I would expect a table to be created called 'tbl_fred' with a PK of 'id' and no errors about not knowing what to do with collections. The above example doesn't work at all, it doesn't even see [Table].

The nearest I have got to working is this:

namespace a_sln
{
  public partial class fred
  {
    public int id {get; set;}
    public List<int> nums {get; set;}
  }
  
  [MetadataType(typeof(fredmetadata))]
  [Table("tbl_fred")]
  public partial class fred
  {
    internal class fredmetadata
    {
      [PrimaryKey]
      public object id {get; set;}
      [Ignore]
      public object nums {get; set;}
    }
  }
}

This acts like it can see the [Table] attribute but not the property attributes. I've tried several variations on a common theme and the results are the same or VS (2017) complaining about duplicates.

I am assuming the nuget package is not at fault by virtue of the fact that the [Table] attribute is working, but I am happy to be corrected.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions