Linq2Sql: linking tables without relationships - linq-to-sql

I'm new to Linq2Sql and to learn it I'm trying to setup reports using Linq2Sql against the FogBugz tables. But, since the FogBugz tables don't have any true Foreign Key relationships, LinqToSql isn't finding the relationships and I can't figure out how to create them through the Designer. So, is there a way to do this manually? If I do it manually and then later decide to add another table, will everything I did be overwritten?

You can add relationships (aka Associations) in the SQL2LINQ designer by right clicking the white space in the designer, choose Add -> Association.
Choose your parent and child tables (classes), then link the properties on the tables accordingly using the grid that appears below the dropdowns for parent and child classes

Related

Symmetric many-to-many relationship in Directus

I have two collections in Directus (data platform that provides many features including REST API over database records). There is option to set relationship between different collections.
I set many-to-many relationship between collectionA and collectionB.
Special system collection (junction table) called collectionA_collectionB was automatically created. Now I can add items from collectionB while editing collectionA items.
But same time I want to add collectionA items while editing collectionB items and I don't find way to add existing junction table. When I add many-to-many relationship between collectionB and collectionA - new junction table is created.
How can I use same junction table to achieve symmetric many-to-many relationship?
From the Directus UI you can delete the relation from CollectionA and also delete the junction table collectionA_collectionB and then create again the M2M to CollectionB but when creating, please click "Continue in Advanced Field Creation Mode" just below the "Save" button you'd like to rush to click otherwise: https://i.imgur.com/7LASmQp.png
Now, click "Relation" from the left menu and under "Corresponding Field" check the "Add M2M" to ColumnB" checkbox: https://i.imgur.com/ptb0aNr.png
If you don't want to delete what's created already (for example, lot of data in the tables already), then I think the only way is to make changes directly to the directus_* tables on the database. I don't know how to do it as I didn't have much data when I had this problem.

Association in Linq to SQL

I have a pretty simple model with a customer and Item, with one-to-many relation between them (One customer can have many items). I used the designer to place my entities and I do see an association between them in the designer. Its xml reflects this:
<Association Name="vgMfiCustomer_vgMfiItem" Member="vgMfiCustomer" ThisKey="CustomerLink" OtherKey="Customer" Type="vgMfiCustomer" IsForeignKey="true" />
Trouble is that in the designer.vb, there's no mention of this association, So I cannot say Customer.Items in my code. Did I miss a step in generating the model? Or maybe I need to add the navigation property manualy? I come from EF.NET background, where the navigation properties are created automatically.
It sounds like you have the association setup for one direction, but not the other. In the dbml, there should be an Association element for each type. You may want to try removing the association in the designer and re-adding it.
Ensure that both tables have a property marked as "Primary key" (in .dbml schema)

Adding to MS Access relationship diagram programmatically

I have an application that ships to the customer with a JET database including a relationship diagram which more savvy users are invited to view to gain insight into the construction of the database.
I also have code in my application to update the database structure when new versions require new tables, new columns, or modified queries. I do this by pushing SQL through the ADO connection it works fine.
The problem is that if I add a new table with a constraint that relates it to an existing table (for instance, I add EmployeeHobbies with an FK relationship to an existing PK in Employees), while the table is constructed correctly the new relationship does not appear in the relationship diagram. Over time the diagram becomes progressively less complete.
Is there a programmatic method to force Access to update its relationship diagram from constraint information in the database or, failing that, is the relationship diagram stored in some hidden system object that I can update directly to reflect my changes?
Edit: I failed to make clear that my application is written in Delphi, not MS Access. Users who have a copy of MS Access can see the relationship diagram, others cannot.
I do not know if RunCommand will suit, but for what it's worth:
DoCmd.RunCommand acCmdRelationships
DoCmd.RunCommand acCmdShowAllRelationships
You may be able to leverage Stephen Lebans' SaveRelationshipView for this. His code saves properties for each item from the Relationships view to a table. Later the same layout view can be recreated from the table.
You could adjust your copy of the Relationships view to determine the values to include for a new row in the tblRelationshipViews table. Then let the users recreate their Relationships view based on the updated table information. Perhaps you could even automate to do it for them automatically.

Delete object and its many-to-many relation

I am trying to delete an object that sometimes has a many-to-many relation. The code I use now is:
db.DeleteObject(registeredDevice);
db.SaveChanges();
This ofcourse just removes the registeredDevice. But usually this device has a many-to-many relation to a project in the database. When trying to delete the device in that scenario it will give an error.
The DELETE statement conflicted with the REFERENCE constraint
What I need to do is remove the device and its relation (the entry in the many-to-many table, not the project it is related to). How do In do this with LINQ ?
To remove everything, you need to make sure all the data is loaded. Basically, linq to sql has to know about the data it is deleting before hand to make the best judgement on how to delete it.
Hoep that helps

Creating an AssociationSetMapping for MySQL with the Entity Framework Designer?

I am trying to build an Entity Framework model for my database schema using EF4 and Visual Studio 2010. Since I am stuck using MySQL as our database (for now), I pretty quickly discovered that since MYISAM tables don't support foreign key constraints, the designer does not have any way to automatically create all the associations for you. It is easy enough to go through the model and build them all manually, but I ran into a problem trying to map a pure join table.
Normally if you are using SQL Server or some other database that supports Foreign Keys, the designer will create all the mappings for you and in the case of pure join tables will create an AssociationSetMapping such that the join table is entirely hidden in the model. Instead you end up with a Many to Many mapping between two two primary entities.
However I am at a loss as to how to create this kind of mapping manually? If I create a Many to Many association between my two entities, I end up with a regular Association, not an AssociationSetMapping. There does not appear to be a way to create one of these in the designer than I can figure out, and to tell it which join table is supposed to be used at the database level.
Am I missing something, and there is a way to do this?
If not, I suppose I have to hack the XML directly, but at this point it is not clear what I need to add to the XML files and where, in order to manually create the association set mapping?
Ok, I can answer this one myself. Once you create a many to many association with the designer, you DON'T set a referential constraint on it, but rather use the Mapping Details window to tell the association what table is the join table, and which keys map to which fields in the database. Simple enough once I figured it out :) But I could not find any good documentation on this.