Solved

Associative table & linq?

Posted on 2011-02-16
5
533 Views
Last Modified: 2012-05-11
Hi!

I've the following schema in my database:

Group:
Id
Name


Rights
Id
Name

AND an associative table
Group_Right
IdRight; IdGroup

Linq generate me for this only two entities, which is right.

I'm using LINQ through wcf, so when I've my objects they are all unlinked and not tracked by the database. I'm using my own POCO entities.

My question is:

When I receive my group and right I should attach to this group, how do I tell linq to save the association?

If have something like

public SaveGroupsRights(Group group, List<Rights> rights){
group.Rights = rights;
DataContext.Save();
}

The association isn't saved in my DB(In this exemple, I've already attached every object to my datacontext).

Thank you!
0
Comment
Question by:Nargzul
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34909561
Using LinqToSQl or RIA?  

You need to add an association between your tables (either in your EDML or DBML).  Also if you plan on using this client side you need to add load options to include the child table data.

0
 
LVL 1

Author Comment

by:Nargzul
ID: 34909704
Nether, Entity Framwork4.0(Linq to entities).

You don't understand, I'm generating this model from the database, which has already all needed foreign keys.

I've already this association, because if I load data from the database, Group_right I set are correctly used to make collections.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34916455
It mentions here that you have to first pull it from the database or update:
http://tonesdotnetblog.wordpress.com/2008/08/15/linq-to-entities-disconnected-crud/

and some more samples:
http://naspinski.net/post/Getting-started-with-Linq-To-Entities.aspx
0
 
LVL 1

Accepted Solution

by:
Nargzul earned 0 total points
ID: 34916558
I already pull them from the database before calling the specified method.

I found a work-around, by calling the _context.ObjectStateManager.ChangeRelationshipState
0
 
LVL 1

Author Closing Comment

by:Nargzul
ID: 34949675
found myself the solution
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now