Additional Attribute

Jun 25, 2008 at 3:22 PM
Hi Matt,

This is great solution. Thank you very much.

I would like to send few more attributes other than table columns. One of the possible way(?) is add a new column in .dbml file, re-run Custom tool to generate code & then remove Column attribute from "additional" column(s) manually (becuase these attributes are not from db.table). But how to fill values into those attributes in single call, as explicit construction is not allowed on Entity type.

[Note: I want to avoid creating my own classes]

Would like to know your thoughts in this. Thanks in advance & keep up the good work.

cheers !!
Jun 26, 2008 at 12:05 PM
Edited Jun 26, 2008 at 12:08 PM


In regards to your question, if the attributes need to be common on every entity, you can create a class with the attributes on it that you want, have it inherit from LINQEntityBase and then have then entites inherit from it (by using the BaseClass XML attribute) - so that this new class is in the middle of the enheritence class (i.e. LINQEntityBase --> [New Class] --> Entity);

If the attributes need to be different on every class, use partial classes instead.

If you don't want to create your own classes, remember that you can use the standard Class Designer Features in Visual Studio on instead of writing them.  Just create a class diagram in your project, you will see the relationships between LINQEntityBase and the Entites.  You won't be able to put attributes on the entites (they will be wiped out every time the code is generated), but you will be able to have partial classes and maintain them without the attributes being wiped out.

In regards to constructors, try something like this:

    Order_Detail order_DetailAdded = new Order_Detail() { UnitPrice = 55,  Quantity = 10,  Discount = 0.15F };

This  is a new feature in .NET 3.5, as you can see you can initialize the values without a constructor in a sincle line.




Jun 26, 2008 at 2:33 PM
Hey Matt...Thanks for quick & detailed response. I guess my problem is a bit different...

Requirement : I want different attributes on every class.

Lets say if I have Entities like 'Customer' --->[1:N] 'CustomerAddress'-->[1:1] 'Country'
'CustomerAddress' table has 'CountryId', which is linked with 'Country' table.

Tasks: I want to return...
1. 'Name' as 'FirstName' + 'LastName'. I have added 'Name' attribute to 'Customer' using partial class.
2. 'CountryName' along with 'CustomerAddress'. I have added 'CountryName' attribute to 'CustomerAddress' using partial class.

Now..question is how to fill these attributes...

1 :
Customer myCustomer ;
myCustomer = (from c in db.Customers where c.CustomerId == 1
                select new Customer
                CustomerId = c.CustomerId,
                FirstName = c.FirstName,
                LastName = c.LastName,
                Name = c.FirstName +
" " + c.LastName

Above query will fail because "Explicit construction in Query is not allowed ".

2. How to write qry to fill 'CountryName' into 'CustomerAddress' ....

Thanks in advance. I hope my questions are not too much off-track than what you are trying to achieve here.

Jun 28, 2008 at 12:37 AM
Edited Jun 30, 2008 at 12:01 AM

I see what you mean now.

Here's some idea's thought I'm not sure if they meet you requirements

1. Add "Name" as a property who's getter puts together this.FirstName, this.LastName on the fly - this would probably be readonly for this case.
2. Same as above with CountryName/Customer Address, except you would go via relations ships to get these, using a query to return the right information.

Hope this helps.



Jun 30, 2008 at 6:53 PM
Thanks Matt.