score:1
Accepted answer
First, if you do not have an additional field in StudentCourse
(such semester of the registration), then you do not need to have StudentCourse
class.
If you want to keep this mode, you can do this:
StudentCourse registration = new StudentCourse();
registration.StudentID = 4;
registration.CourseID = 6;
context.StudentCourses.Add(registration);
context.SaveChanges();
These resources may give you further explanation:
If you want to check if there is no duplicate, you can simple do these:
if(context.Courses.Where(c => c.Title == 'Math').FirstOrDefault() == null)
{
//add the course
}else {
//already existing
}
Source: stackoverflow.com
Related Query
- updating data in many-to-many relationship in entity framework in code first existing database
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- many to many mapping in entity framework code first
- How to Query Icollections of Entity Framework Code First Data
- Entity Framework code first - Many to Many - Include conditional
- Why Entity Framework Code First one to many Doesn't work properly
- Entity Framework Code First Select Item Based on Relationship
- How to join columns or properties of entities that belongs as many relationship to another entity in .NET EF Code First
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Filtering Data in Many To Many Relationship in Entity Framework
- Entity Framework 6 Code First Custom Functions
- Entity Framework Code First without app.config
- Query a many to many relationship with Entity Framework and .NET 3.5
- Entity Framework Code First using context in Controller
- How to Create a Run-Time Computed (NotMapped) Value in Entity Framework Code First
- Entity Framework Code First String Comparison with Oracle Db
- SQL subquery result in LINQ and Entity Framework Code First
- Entity Framework foreign key relationship returns no data
- most efficient Entity Framework Code First method of flattening / projecting parent entity with specific child
- LINQ query help: searching for data in a Many to Many using Entity Framework
- Entity Framework Code First - Get blog posts which have certain tags
- Entity Framework Code First ToList method timing out on SQL Azure
- How to make a property unique in Entity Framework Code First
- Entity Framework Code First override onModelCreating() for TPT Inheritance Screwing Up Identity User and Role Models
- How to loop through a child list of objects in Entity Framework 4.1 code first
- How to implement self-recursive parent-child relationship data loading with eager loading policy in Entity Framework Core 2.2?
- how to write a Linq query with a EF code first Many to Many relationship
- linq to one to many relationship Entity framework
- LINQ - Entity framework code first - Grouped results to custom class
- map one to one with multiple primary key columns entity framework code first
More Query from same tag
- LinQ WHERE string.Contains or string.IndexOf?
- Linq Select Expression this keyword
- MSDTC Distributed Transaction Coordinator Enabling
- Skip all items after first one with same id
- How ToLookup() with multiple indexes?
- Dynamic Linq/Lambda Filtering
- How to write some LINQ which excludes related records in a link table?
- How to handle casting delegate of anonymous type <T>, to delegate of T for use in Where<T>() method of an IEnumerable<T>
- How to sort in LINQ If Join other database
- Finding if value exists in list with LINQ and saving info into a dataset, possibly
- Linq to NHibernate and Dynamic LINQ - query caching not working
- Find the category which has 3 or more than 3 products data by using the linq and lambda query
- How to use FirstOrDefaultAsync() in async await WEB API's
- Linq query where in list, performance what is the best?
- Where-clause for LINQ-to-NHibernate FetchMany?
- How to sort text file by Column with LINQ using start and end column positions
- Difference Between Select and SelectMany
- C# LINQ left join 2 Datatables
- C# XML documentation of Linq to SQL
- Improving a method by using LINQ
- Need to pass a generic filter function to list of objects to create where clause
- How to get list of members in particular group using LINQ in Umbraco
- bad performance when contains all query in entity framework
- Cannot implicitly convert type 'Anonymous Type#1[]' to 'int[]'
- ASP.NET MVC Ajax - InvalidOperationException, sequence contains no elements
- how can I use Moles to redirect select from tables via LINQ?
- C# Select View from Oracle DB with LINQ
- MVC ASP C# My DeviceID is not displaying on my views
- Managing ranges with LINQ challenge
- EF/LINQ: Where() against a property of a subtype