1
Vote

Why my relationships don't work

description

Hi I'm playing with this and it looks like just what I want ..
but I just can't seem to get the relationships working properly as expected.
 
I've attached a sample project, and DB scripts, .. I'm sure it's something simple, but the only way I've been able to get the HasMany relationship to populate anything is to change the IList<source_alternative_name> to an EntitySet<source_alternative_name> .. this seems to populate it, but I want it to lazy load and the back references still don't seem to work.
 
please see attached for a solution and DB scripts
 
here's the basic code I'm using ..
 
can't really see what I'm doing wrong
 
public class Source
{
    public Source()
    {
        _source_alternative_names = new EntitySet<SourceAlternativeName>(altname => altname.source = this, altname => altname.source = null);
    }
    public int id { get; set; }
    public string name { get; set; }
    private EntitySet<SourceAlternativeName> _source_alternative_names;
    public IList<SourceAlternativeName> source_alternative_names
    {
        get { return _source_alternative_names; }
        set { _source_alternative_names.Assign(value); }
    }
 
}
 
public class SourceAlternativeName
{
    public int id { get; set; }
    public string name { get; set; }
    public int source_id { get; set; }
    private EntityRef<Source> _source;
    public Source source
    {
        get { return _source.Entity; }
        set { _source.Entity = value; }
    }
}
 
public class source_mapping : Mapping<Source>
{
    public source_mapping()
    {
        Named("source");
        Identity(x => x.id);
        Map(x => x.name);
        HasMany(x => x.source_alternative_names).OtherKey(y=>y.source_id);
    }
}
 
public class source_alternative_name_mapping : Mapping<SourceAlternativeName>
{
    public source_alternative_name_mapping()
    {
        Named("source_alternative_name");
        Identity(x => x.id);
        Map(x => x.name);            
        BelongsTo(x => x.source).ThisKey(x=>x.source_id);
        Map(x => x.source_id);
    }
}
 
public static class static_data_service
{
    public static List<Source> GetAllSources()
    {
        using (var FluentTest_data = new FluentTest_db_context())
        {
            var sources = from s in FluentTest_data.GetTable<Source>() select s;
            return sources.ToList();
        }
    }
 
    public static List<SourceAlternativeName> GetAllSourceAlternativeNames()
    {
        using (var FluentTest_data = new FluentTest_db_context())
        {
            return (from s in FluentTest_data.GetTable<SourceAlternativeName>() select s).ToList();
        }
    }
 
}
 
public class FluentTest_db_context : DataContext
{
    private static readonly string connectionString = ConfigurationManager.ConnectionStrings["FluentTestConnectionString"].ConnectionString;
    private static readonly MappingSource mappings;
 
    static FluentTest_db_context()
    {
        mappings = new FluentMappingSource("FluentTest")
            .AddFromAssemblyContaining<source_mapping>()
            .CreateMappingSource();
    }

    public FluentTest_db_context(): base(connectionString, mappings)
    {
 
    }
 
 
 
}

file attachments

comments

JeremyS wrote Jun 26, 2010 at 8:04 AM

You need to tell Linq to Sql the underlying storage for the association properties. As your properties are type IList, it will not enable lazy loading. Linq to Sql supports the "storage" attribute for doing this. With Fluent you can say HasMany(x => x.Foo).Storage("fieldname"). Alternatively you can use one of the mapping modifications to enable this for all associations.

vurt007 wrote Jun 26, 2010 at 8:59 AM

Ah I see, .. have seen that storage attrib in in auto generated linq to sql mapping things to private members ..
Can you explain what you mean by the ' use one of the mapping modifications to enable this for all associations.' ?

wrote Feb 13, 2013 at 2:55 AM