views:

472

answers:

3

Is it possible to add the "DeleteOnNull=true" on a custom class instead of modifying the DBML (generated) class directly?

For example, let's say this is a part of my generated dbml class:

[Table(Name="OrderDetails")]
public partial class OrderDetail : 
       INotifyPropertyChanging, INotifyPropertyChanged
{
    // deleted for brevity

    [Association(Name="Order_OrderDetail", 
         Storage="_Order", ThisKey="OrderId", 
         OtherKey="OrderId", IsForeignKey=true, DeleteOnNull=true)]
    public Order Order 
    {
        get { /* deleted */ }
        set { /* deleted */ }
    }
}

So is it possible to put the "DeleteOnNull=true" on a separate class? Is it is? How? I have tried the following without any luck:

[MetadataType(typeof(OrderDetailMetadata))]
public partial class OrderDetail {
    internal sealed class OrderDetailMetadata
    {
        [Association(DeleteOnNull = true)]
        public object Order;
    }
}
A: 

I'd build the assembly then use reflector to example the class to see if it has the attribute properties set from the partial class. If that don’t even work then you may need to try something else. That type of attribute overlap may not be supported.

What is it you're trying to achieve thru this?

cottsak
Basically trying to minimize in keeping editing the dbml and re-adding the DeleteOnNull everytime we change the DB schema and regenerate. So we can keep regenerating the LtS dbml during db development and the custom code (in this case the DeleteOnNull) won't be impacted.
Johannes Setiabudi
Yeah i see your problem. Bummer. Sorry dont know about this one.
cottsak
+1  A: 

Maybe you dont need the MetaData type. Will this work in your additional partial:

public partial class OrderDetail {
    [Association(DeleteOnNull = true)]
    public Order Order;
}

..i know that by itself Order is not defined, but you shouldn't need to build like that. This MSDN doc suggests that the above should be possible (if i'm reading it correctly).

cottsak
It is possible if Order is not a property generated class in the dbml. But in my case, OrderDetail is just like any other table that is dragged into the designer into code-generation. So the property Order inside OrderDetail class is also exist via generation. If I do what you recommend, it will create duplicate declaration of the property "Order" inside the OrderDetail class.
Johannes Setiabudi
Yeh.. of course. Mmmm.. did u try it? just to verify that there's no preference system (tried looking for some doco about preference on the same attributes - didnt find any).
cottsak
Yes I did try it. As expected, got a compile error
Johannes Setiabudi
The Association keywords conflicts with the System.ComponentModel.DataAnnotations namespace. This may work better:[System.Data.Linq.Mapping.Association(DeleteOnNull=true)]public Order Order;
jocull
A: 

Better late than never:

If you have been using the Designer to create your LTS config and entities, you can right-click on the DBML file, and choose "Open With...". Now select XML Editor and click OK.

Find your Order_OrderDetail association in this file, then add DeleteOnNull="true" to it. Save the file and let LTS re-generate your classes for you. Done! No more having your changes to the generated code file overwritten again!

Funka