codingdir logo sitemap sitemap |

EF 5. Code First DbContext Child objects not mapping on retrieving from DB

By : , Category : c#

You forgot to mark them as virtual.

 public virtual Region ParentRegion { get; set; }

This is how you let EF to create overridden properties within autogenerated proxy classes that lazy load your parent entities.

ReLated :

found a solution,using ForeignKey in child mapping to parent enabled use of both methods.

<System.ComponentModel.DataAnnotations.Schema.ForeignKey("parent")> _
Public Property parent_id As Long
<Xml.Serialization.XmlIgnore> _
    Public Property parent As parent


If we do not want to map separated entity, we cannot use one-to-many. We need:

6.3. Collections of Values and Many-To-Many Associations (small cite)

For a collection of values, we use the tag.

        column="column_name"                (1)
        type="typename"                     (2)


A list of components (discussed in the next chapter):

<list name="CarComponents" table="car_components">
    <key column="car_id"/>
    <index column="posn"/>
    <composite-element class="Eg.Car.CarComponent">
            <property name="Price" type="float"/>
            <property name="Type" type="Eg.Car.ComponentType, Eg"/>
            <property name="SerialNumber" column="serial_no" type="String"/>

So in our case, we cannot use one-to-many but Component:

// instead of this
r =>{ r.OneToMany();}
// we need this for IList<string>
r.Element(e => { });
// this for DataSource as in the above question
r.Component(c => { });


The mapping by code should go like this: Hibernate's Mapping by Code. Any entity should have surrogate key (Id). That should be the DataSource:

public class DataSource
   public virtual int Id { get; set; }
   public virtual string InternalRefNr { get; set; }
   public virtual MyDataMyData{ get; set; }

The mapping of that class then could be:

public class DataSourceMap : DataSourceMapping<DataSource>
    public DataSourceMap()
        Id(x => x.Id, m => m.Generator(Generators.Identity));
        Property(x => x.InternalRefNr);
        ManyToOne(x => x.MyData);

And now we can map MyData based on Mapping-by-Code - Set and Bag

public MyData()
    Bag<DataSource>(x => x.Sources,
        c => { c.Inverse(true); },
        r =>{ r.OneToMany();}

You need to use the create option to tell the mapping plugin how it should map your child property.

So if you want to have having Object and null you need to return null when your child property is null:

var mappingOptions = {
    child: {
        create: function(options) {
            if (!
                return null;
            return ko.mapping.fromJS(;

ko.mapping.fromJS(data, mappingOptions,;  

Demo JSFiddle.

Or if you want them both Observables:

var mappingOptions = {
    child: {
        create: function(options) {
            return ko.observable(ko.mapping.fromJS(;

ko.mapping.fromJS(data, mappingOptions,;   

If you know at compile time the name of the method you're calling, you don't have to use reflection. It sounds like you need reflection. However, that doesn't mean you have to use the raw java.lang.reflect API, which is pretty painful to use directly. If you're already using Spring, BeanWrapperImpl is a very nice utility that can use your existing ConversionService to convert your input to the target type. Otherwise I'd suggest Commons BeanUtils, which does pretty much the same thing without the Springiness. Both libraries know how to handle nested properties and Map-valued properties as well.


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .