This is a migrated thread and some comments may be shown as answers.
Virtual properties
1 Answer 35 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alexander Timofeev
Top achievements
Rank 1
Alexander Timofeev asked on 31 Oct 2012, 02:49 PM
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.

1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 05 Nov 2012, 01:49 PM
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.
Tags
Data Access Free Edition
Asked by
Alexander Timofeev
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or