This is a migrated thread and some comments may be shown as answers.

MappingConfiguration class per entity and complex types?

2 Answers 100 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Diego
Top achievements
Rank 1
Diego asked on 31 Aug 2013, 10:15 PM
I'm sure i'm missing something pretty silly here but I keep getting the following error... please help.. Also, how do you map complex types?

This is against the northwind sample db..

System.MissingFieldException : There is no field with name 'CustomerID' backing 'Id' property in type 'MMG.Core.Testing.Integration.Northwind.Customer'. 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.

Code:
public class CustomerOAMapping : MappingConfiguration<Customer>
    {
        public CustomerOAMapping()
        {
            MapType().ToTable("Customers");

            HasProperty(p => p.Id).HasFieldName("CustomerID").IsIdentity().HasLength(5);
            HasProperty(p => p.Name).HasFieldName("CompanyName").HasLength(40).IsNullable();

            //contact complex
            HasProperty(p => p.Contact.Name).HasFieldName("ContactName").HasLength(30);
            HasProperty(p => p.Contact.Title).HasFieldName("ContactTitle").HasLength(30);
            HasProperty(p => p.Contact.Phone).HasFieldName("Phone").HasLength(24);
            HasProperty(p => p.Contact.Fax).HasFieldName("Fax").HasLength(24);

            //address complex
            HasProperty(p => p.Contact.Address.Street).HasFieldName("Address").HasLength(60);
            HasProperty(p => p.Contact.Address.City).HasFieldName("City").HasLength(15);
            HasProperty(p => p.Contact.Address.Region).HasFieldName("Region").HasLength(15);
            HasProperty(p => p.Contact.Address.PostalCode).HasFieldName("PostalCode").HasLength(60);
            HasProperty(p => p.Contact.Address.Country).HasFieldName("Country").HasLength(15);
        }

    }
}

2 Answers, 1 is accepted

Sort by
0
Diego
Top achievements
Rank 1
answered on 03 Sep 2013, 03:15 PM
It may be related to this - http://www.telerik.com/community/forums/orm/development/problem-with-fluent-mapping-api-transient-properties-not-work-working-q1-2011.aspx#1688829

??
0
Kristian Nikolov
Telerik team
answered on 04 Sep 2013, 03:33 PM
Hello Diego,

Most probably the reason for the error you are getting is incorrect field name passed to the HasFieldName() extension method. This method requires the name of the field which is accessed through the property specified in the HasProperty() method. So in your case you must pass the actual name of field accessed through the Id property.

Note that after fixing the field names, your code will still result in an error due to the fact that  OpenAccess will search the fields of Contact and Address in the Customer class. You can however take advantage of our struct support and define Address and Contact as structures. This is the OpenAccess ORM feature that matches the Entity Framework Complex Types support. I have attached a sample project illustrating how to map Customer class that has properties of types Address and Contact which are defined as structures. With this approach however, the Address and Contact objects will not be persistent, they will be used solely to organize the data contained within Customer objects. Please note that in order to run the project you will need to upgrade the project references and change the connection strings located in both app.config files.

In case you wish to persist Address and Contact, you may try to organize your classes in a hierarchy and use Flat Inheritance, with which all types in the hierarchy are mapped to the same table.

I hope the provided information will help you with your scenario.

Regards,
Kristian Nikolov
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Tags
Getting Started
Asked by
Diego
Top achievements
Rank 1
Answers by
Diego
Top achievements
Rank 1
Kristian Nikolov
Telerik team
Share this question
or