Renaming SQL LINQ 2 Object Properties Through Partial Classes

Can partial classes be used to create properties that point to an association property created by the L2S designer. Also, can I use the new property in queries?

How can i achieve this?

+1
rename linq-to-sql naming
source share
2 answers

Yes you can, but you must apply the same attributes as the generated linq2sql property ie

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

TestDatas is the original relation.

Update: A sample request that I executed:

  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(); } 
+1
source share

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

EDIT . The disadvantage of using a separate property in a partial class is that LINQ will not be able to use it when generating queries - in essence, you will always have to get objects before you can use related properties on the object. Renaming allows LINQ to use related properties when building a query, which can lead to a more efficient query. For example, if you want to get objects for which the associated object has a specific property value, using an attribute decorated with an entity will allow LINQ to generate SQL to pull only those corresponding values ​​from the database. With a naive implementation of a property (which simply refers to a property of the underlying relationship, actually renaming it), you will be forced to first get all the entities and then perform filtering in your application.

+2
source share

All Articles