I am working on some CRUD methods for a web app. I am using Entity Framework with SQL Server 2008. In particular, I have two tables that are linked using a cross reference table. It's in the following format:
Table Tbl_Plan
PlanId
PlanNm
Active
Table Tbl_Person
PersonId
PersonNm
PersonData
Xref table Xref_PersonPlan
PersonId
PersonNm
Now, when I am creating a new plan I have been trying to link the two together. I'm not sure how. I know it has something to do with referencing the relationship or creating it.
The tables are set up correctly, because I can pull the data just fine once it's created (i.e. my sample entries in SQL Server) but creating the reference is where I am stumped. I tried the following:
using (Entities context = new Entities())
{
// TblPlan plan has already been instantiated
plan.TblPersons.Add(person);
context.AddToTblPlans(plan);
context.SaveChanges();
But obviously, the .Add() isn't what I'm looking for....help?
The short answer is that many-to-many relationships are not supported in LINQ to SQL the same way they are in Entity Framework and other ORMs, and you'll have to implement the desired behavior yourself.
Take a look at this article. It describes an approach that should work for you.
Someone just pointed me in the right direction. I should have waited ten more minutes. I was missing the .AttachTo() - I needed to attach the reference to the context before adding, since it was throwing an InvalidArgumentException.
Related
I have an odd situation. I am working on a project with a very large existing database that is completely unrelated, but does contain corresponding table id's. It's as if someone copied the database but never related the tables.
In Entity Framework, is there a way to go EF code first and create the relationships in code, but Not apply those relationships in the database? I would like to go through and relate the database but the client doesn't want to pay to fix it.
Thanks!
In this instance, it seems you would be best to add relationships directly to your database (or to a duplicated database for testing/staging) and then just update your entities using your test connection and regression test your app.
I tried finding the answer to this question before posting, but couldn't.
We are using a remote database with Entity Framework and I do not know the approach it was used to create the database. What I would like to do is add a class to the project, add the class to DbSet an create the table automatically. Now, when I use update-database in nuget, i get "migrations not enabled for this database" and its true, there is no configuration file or anything that suggest the approach it was used for creation of this.
I wouldn't want to enable-migrations as I don't want to mess something up or loss data. (or should i?) The existing tables are working fine with the repository...
I created a table manually, added a class that maps the properties by name and hoped that entity framework will pick it up, but no luck.
Here it is in a nutshell: I want to add a new table in a remote database that will be picked up by Entity Framework an generate a class for me. There is also NO .edmx file that can update the model. How was this done then... (?)
I am a new to Entity Framework, so apologies it some of this does not make sense. I am happy to clarify.
Thanks,
Thank you for your responses. I managed to fix this by adding a new class into the repository, using code first approach and then I created a table manually in the database.. The Entity Framework pick it up somehow :)
I was doing this before, but the problem was that I had the DbSet name to plural and database to singular. For instance: Products was the DbSet name and Product table in database. The actual class is still singular.
Thanks again!
I am uploading a lot of data to a database using entity framework. I have a lot of different entities with relations between them.
My problem is that sometimes the object I'm uploading might already be in the database, but when I look up that object and find it, I can't add it to my locally made entities, because they belong to different contexts.
For example, I have the entities Sailor and Booze, which have a relation. I have a new sailor Ackbar and I know his favourite booze is rum and I want to persist this to the database.
I make a new sailor and set its name to Ackbar. Then I look up to see if Booze has an entry called rum. If it has, I try to add it to Ackbar. When I do this, EF complains that the new sailor and the booze from the database belong to different contexts.
If I try to attach sailor to the context, it complains that sailor has a null entity key.
How can I build all these relations without saving anything to the database before I'm done editing the relationships?
I suggest that you alter your code to use the same Context for reading and writing. Having multiple contexts for a single transaction is not a better option than having a Context that's alive for a few minutes.
I have a database that relies on a bunch of Many-to-Many and rather than denormalizing the whole database (which is quite large) I would like to know if there is a SIMPLE way of implementing Many-to-Many Relationships using LINQ-to-SQL Yet...
Everywhere I look I find articles on HOW Difficult It is, or A Simple Workaround that is actually "NOT" Simple. But all the articles I have been able to locate are pretty dated, using asp.net 3.5.
Should I just Create a Stored Procedure and then Drag it into the .DBML file as a method?
So I guess my question is:
Is there any new way to implement many-to-many using Linq-to-Sql in ASP.NET 4.0 along with MVC 3.
Examples of usage in my Project:
In the finished model, there will be Other Tables linked to the Many-to-Many Tables (i.e. EventAddress, EventUrl, VipAddress, VipPicture, GuestAddress, GuestPhone, GuestEmail, etc...)
So you can see when this would be a pain in the a$$ to denormalize.
Thank you,
Tim
Have you considered using Entity Framework?
It handles m:n relationships nicely. In your case the EmployeeAddress table would be abstracted away as a list of addresses on your Employee object.
It is also my impression that linq to sql isn't really a priority for Microsoft, EF Is.
I understand if you do not wish to convert an existing project, but for new projects EF4 would probably be a better fit.
LINQ to SQL will always map directly to the structure of the database. If your database has all of those junction tables, then they will be in your LINQ to SQL model - period.
Although you can map many-to-many relationships in both LINQ to SQL
and LINQ to Entities, LINQ to Entities allows a direct mapping of
many-to-many relationships with no intermediary class, while LINQ to
SQL requires that an intermediary class map one-to-many to each of the
classes that are party to the many-to-many relationship.
link to the quote above - http://msdn.microsoft.com/en-us/library/cc161164.aspx
I have a application which is running in both production and development environments. I would like to utilize the databases better (and save money on my hosting bill) so i want to be able to make my Linq2Sql run on two different schemas (instead of two different databases) (there are ~15 tables in a schema). How to set this up in Linq2Sql?
Or should i go the distance and read up on Entity Framework instead (and is it possible to segment the tables based on schemas in this one?
Any other solutions to this problem are welcome?
This is actually easier to do in LINQ to SQL than it is in EF. Not that it is terribly easy, mind you. I wrote a blog post a couple of years ago on how to do it but the heart of it is to specify the mapping source in your context constructor.
XmlMappingSource source = XmlMappingSource.FromUrl("TestLINQ.map");
// Could also use XmlMappingSource.FromXml(string)
using (LINQ.TestLINQDataContext context = new LINQ.TestLINQDataContext(Properties.Settings.Default.TestConnectionString, source))
{
Using this method, you can alter your mapping source to point at the schema (or table name) that you want to.