1

Can I use partial classes to create properties that points to an association property generated by the L2S designer. Also, will I be able to use the new property in queries?

How can I achieve this?

Ali Kazmi
  • 3,610
  • 6
  • 35
  • 51

2 Answers2

2

If you just want to give a different name to the association property, just use the Property page for the association and rename the parent and/or child property. That will change the name of the EntityRef/EntitySet in the class.

EDIT: The downside of using a separate property in a partial class is that LINQ won't be able to use it when generating queries -- essentially you'll be forced to always get the entities before you can use the related properties on the object. By renaming you allow LINQ to use the related properties in constructing the query which can result in a more efficient query. For example, if you want to get entities where a related entity has a particular property value, using the attribute decorated entity will allow LINQ to generate the SQL to pull just those matching values from the database. With the naive property implementation (that simply references the underlying relation property, in effect renaming it), you will be forced to first get all entities, then do the filtering in your application.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • This is not a practical approach. The changes are overridden when I regenerate the model :( – Ali Kazmi Apr 12 '09 at 14:56
  • +1 it is a practical approach. Linq2sql doesn't have a regenerate model button, doing changes in the designer is part of working with it. That said will add an answer for more info on your question. – eglasius Apr 12 '09 at 14:59
  • By regenerate, do you mean remove the table and add it back in? Yes, that is a problem, but surely that doesn't happen very often. Or are you using an external mapping file? I don't have any experience with that. – tvanfosson Apr 12 '09 at 15:00
  • You can reference the loaded properties, but if you attempt to select on a value of one of the properties in the indirect reference, it won't be able to translate it directly to SQL. – tvanfosson Apr 12 '09 at 15:13
  • Try adding a where clause "where d.MyTestData.ID == 1" or something like that. – tvanfosson Apr 12 '09 at 15:16
  • ...sorry wrong wording in my comment. I didn't mean "selection" I meant the where predicate that does the selection based on the child/parent property. – tvanfosson Apr 12 '09 at 15:17
1

Yes you can, but you have to apply the same attributes as the linq2sql generated property i.e.

    [Association(Name="Test_TestData", Storage="_TestDatas", ThisKey="SomeId", OtherKey="OtherId")]
    public System.Data.Linq.EntitySet<TestData> MyTestDatas
    {
        get
        {
            return this.TestDatas;
        }
    }

TestDatas being the original relation.

Update: A sample query I ran:

        var context = new DataClasses1DataContext();
        var tests =
            from d in context.Tests
            where d.MyTestDatas.Any(md=>md.MyId == 2)
            select new
            {
                SomeId = d.SomeId,
                SomeData = d.SomeData,
                Tests = d.MyTestDatas
            };
        foreach (var test in tests)
        {
            var data = test.Tests.ToList();
        }
eglasius
  • 35,831
  • 5
  • 65
  • 110
  • This won't work the same way if the parent/child relation is used in queries since the indirect property won't be supported for expression translation. – tvanfosson Apr 12 '09 at 15:08
  • I just ran 2 sample queries :) – eglasius Apr 12 '09 at 15:09
  • @Freddy -- you can reference the loaded properties, but I don't think it will work for selection's. Without the attribute decoration I don't think it has the hints required to generate the expression. – tvanfosson Apr 12 '09 at 15:15
  • @Freddy -- see my correction on my answer -- I mean in the where clause. – tvanfosson Apr 12 '09 at 15:19
  • Correct, it won't work for where clauses, in which case the only way would be to replicate the attributes. – eglasius Apr 12 '09 at 15:29
  • @tvanfosson thx, updated my answer based on that. In the samples I ran the extra pieces were run after the initial query, which doesn't happen with normal relation properties. Somehow I though it had to do with entity sets or something special ... back on the right track now :) – eglasius Apr 12 '09 at 15:42
  • 1
    @eglasius - Your approach was giving me error "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported." when I attempted InsertOnSubmit() on datacontext. I guess the attribute needs to be removed for submits. –  Oct 21 '11 at 06:16