Virtual properties

2 posts, 0 answers
  1. Alexander Timofeev
    Alexander Timofeev avatar
    1 posts
    Member since:
    Aug 2012

    Posted 31 Oct 2012 Link to this post

    Hi.

    Does OpenAccess Free Edition support C# virtual properties?

    The simple test example is not working. I have compile time error:
    Error 23 There is no field with name 'name' backing 'Name' property in type 'Roof.ExtendedModel.Entities.TestDerived'. You need to either change the Field Naming rules of the mapping configuration object or call HasFieldName(string) with the name of the backing field.

    public class TestBase
     {
         public Int64 Id { get; set; }
     
         public virtual string Name
         {
             get
             {
                 return "";
             }
             set
             {
             }
         }
     
         public new static MappingConfiguration GetMapping()
         {
             MappingConfiguration<TestBase> m = new MappingConfiguration<TestBase>();
             m.MapType();
     
             m.HasProperty(p => p.Id).IsIdentity(KeyGenerator.Autoinc);
     
             m.HasProperty(p => p.Name).AsTransient();
     
             return m;
         }
     }
     
    public class TestDerived : TestBase
     {
         public override string Name
         {
             get
             {
                 return "";
             }
             set
             {
             }
         }
     
         public new static MappingConfiguration GetMapping()
         {
             MappingConfiguration<TestDerived> m = new MappingConfiguration<TestDerived>();
             m.MapType().Inheritance(InheritanceStrategy.Vertical);
     
             m.HasProperty(p => p.Name).AsTransient();
     
             return m;
         }
     }

    Alexander.
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 05 Nov 2012 Link to this post

    Hello Alexander,

     OpenAccess currently supports two types of properties:
    1. Properties being backed by fields.
    2. Auto properties.
    If you are having a non auto property that is not backed by a field you will be seeing that exception. The simple workaround for your scenario would be to cache the returned value in a field and return the field instead. Your code should look something like this:

    private string name = string.Empty;
    public virtual string Name
         {
             get
             {
                 return this.name;
             }
             set
             {
             }
         }

    All the best,
    Petar
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
Back to Top