Solved

Associative table & linq?

Posted on 2011-02-16
5
532 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 39

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 39

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 24
Image(7) 1 33
String manipulation 15 49
How to LOAD up a .NET Framework website with data? 2 8
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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

12 Experts available now in Live!

Get 1:1 Help Now