Asked by:
insert for many to many relation in entity framework

Question
-
User87195929 posted
hi team.
i am unable to insert the many to many relation useing
navigation property for 2 table..
if i have 2 table like student and hobbies.
studenthobbies contain primaray key of 2 table.
how can i insert via navigation property for studenthobbies table...or give some link
Monday, February 8, 2021 1:14 PM
All replies
-
User475983607 posted
The community cannot provide guidance without your code. Is there anyway you can share sample code that illustrates the problem?
Otherwise, inserting records in Entity Framework is covered in the official documentation. It does not matter if you are inserting one record or many records the code is same.
Monday, February 8, 2021 1:40 PM -
User1535942433 posted
Hi siddangoud,
According to your description,I think you need insert data in tables that has a relationship many-to-many.
You need to map entities add navigation property to represent that relation.And then you could insert data into it.
More details,you could refer to below article:
https://www.codeproject.com/Tips/893609/CRUD-Many-to-Many-Entity-Framework
Best regards,
Yijing Sun
Tuesday, February 9, 2021 3:58 AM -
User87195929 posted
Hi
i am ref this link
https://www.codeproject.com/Tips/893609/CRUD-Many-to-Many-Entity-Framework
but in insert method
for naviagation property is not working for insert
my question samle like
https://forums.asp.net/t/2152629.aspx?Need+help+with+MVC+Create+Edit+form+for+many+to+many+relationships
Tuesday, February 9, 2021 7:20 AM -
User475983607 posted
Saving data is trivial. Share your code so we can see what you are doing wrong.
Tuesday, February 9, 2021 12:12 PM -
User1535942433 posted
Hi siddangoud,
Hi
i am ref this link
https://www.codeproject.com/Tips/893609/CRUD-Many-to-Many-Entity-Framework
but in insert method
for naviagation property is not working for insert
my question samle like
https://forums.asp.net/t/2152629.aspx?Need+help+with+MVC+Create+Edit+form+for+many+to+many+relationships
According to your description,I don't understand your requirement clearly.You need many To many relationships in Entity Framework Core?If you need this,you could use the Fluent API. If not,you could post your codes and tell us more details of requirements to us.It will help us to solve your problems.
More details,you could refer to below article:
https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration
Best regards,
Yijing Sun
Wednesday, February 10, 2021 5:39 AM -
User-1044770503 posted
Hi,
Check the following page with the sample code to create, read, update and delete records in EF many to many relationships
Friday, March 19, 2021 3:07 PM -
User503812343 posted
you will have to configure model entities OnModelCreating event of DBContext
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<DeptEmployee>() .HasKey(de => new { de.EmployeeId, de.DepartmentId }); } public DbSet<Employee> Employees { get; set; } public DbSet<Department> Departments { get; set; } public DbSet<DeptEmployee> DeptEmployees { get; set; }
for more information see https://geeksarray.com/blog/how-to-configure-entity-relationships-using-fluent-api-in-entity-framework
Tuesday, March 23, 2021 5:57 AM