Linq and DB connections
Quick question here guys.
I'm working with an older database that didn't have any relationship and now I'm trying to make it as consistent as possible.
the code I was migrating had some quirks that resulted in some situations in which I could not enforce a relationship (PK ↔ FK). I was wondering if this is a coercive relationship a requirement for Linq to SQL?
thanks for the reference :)
when I commented out one of the possible answers ...
the question is much simpler:
Does linq allow to get data about links on which consistency is not satisfied? I'm not talking about adding additional props ...
this would mean functionality for values that have the correct PK ↔ FK relationships I think (as I said, I haven't tested this, maybe some of you?). and what will this do for values that do not exist? to raise the zero value?
a source to share
The auto-generated LINQ to SQL classes (found in the .cs file under the dbml) are partial , so you can add additional properties and functionality as needed.
Explicit relationships in SQL are used by LINQ to add properties - for example, if you have a Customers table and an Orders table with explicit relationships, LINQ will be able to put a strongly typed Orders collection as a property on the Customer Object. Without this, it can only walk to the CustomerID object (int or any other) of the Order object.
In the absence of an explicit relationship, you can manually add properties and attributes to the automatically generated entity classes. I highly recommend putting as much as you can in the partial class so that the changes are not lost if you re-generated the entity classes.
a source to share
Database relationships are optional. It is used by the designer to display the "properties" relationship that you get automatically, but you can add your own and specify which scalar properties define the relationship in both tables. (Right click on designer, select Add Relationship and define both tables and columns, or drag / drop with the Relationship tool)
Be careful. If your problem is that your application stores "placeholder values" to identify "no relationship" (for example, a null value that has no counterpart in the primary key table), you might get exceptions if you try to access the corresponding member in the code.
If the problem is that the original application stores stuff in the wrong order (therefore preventing you from enforcing FK constraints), but when all is said and done, you have consistent data, then you shouldn't have a problem.
a source to share