-1

Entities

College

Course

CollegeCourses'

I have 2 database tables:

  1. College
  2. Course

Now I want to create an additional table for CollegeCourses but it should have also 2 foreign keys from College and Course table.

If I want one foreign key on a table then I can do like this:

modelBuilder.Entity<Course>().OwnsMany(f => f.CollegeCourses);

I'm not sure on what to do if I try to add two foreign keys in one table.

I tried this one but it didn't work, it creates another table instead of just CollegeCourses:

modelBuilder.Entity<College>().OwnsMany(f => f.CollegeCourses);
modelBuilder.Entity<Course>().OwnsMany(f => f.CollegeCourses);

SOLVED: Thanks for the help guys!

I found this on the link provided by @Hassan: Fluent API, many-to-many in Entity Framework Core Sample Code'

I'm a bit confused on why there are two foreign key as a primary key on a table. How do I get those data without an Id holding them?

And I created a sample based on the code above but the difference is that I added a variable Id as my primary key.

modelBuilder.Entity<CollegeCourse>().HasKey(pc => new { pc.Id });

  modelBuilder.Entity<CollegeCourse>()
                .HasOne(pc => pc.College)
                .WithMany(p => p.CollegeCourses)
                .HasForeignKey(pc => pc.CollegeId);

  modelBuilder.Entity<CollegeCourse>()
                .HasOne(pc => pc.Course)
                .WithMany(c => c.CollegeCourses)
                .HasForeignKey(pc => pc.CourseId);

The database sample table look like this: Table

  • Your `CollegeCourse` table needs to have two tables IDs, To have a better understating [read this](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#basic-many-to-many) – Hassan Monjezi Jun 17 '23 at 13:39
  • @HassanMonjezi I just want my table to be something like this: `create table CollegeCourses( Id PK, CollegeId FK, CourseId FK)` I check your link but I'm a bit confused. Sorry I'm just new to wed dev – Gerald Flores Jun 17 '23 at 15:03
  • Please include the actual code instead of images. – Eldar Jun 17 '23 at 15:56
  • @GeraldFlores this thread will help you https://stackoverflow.com/questions/46184678/fluent-api-many-to-many-in-entity-framework-core – Hassan Monjezi Jun 17 '23 at 18:54
  • 1
    @HassanMonjezi, Bro! Thanks for the link. It works! On the link, the table only consist of two foreign key and all I did was add an Id as primary key and two foreign key. – Gerald Flores Jun 18 '23 at 14:44

1 Answers1

0

You must change delete behavior of one foreign key to Restrict. I create example: First create your classes:

public class Customer
{
    public string Id { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string ZipCode1 { get; set; }
    public string ZipCode2 { get; set; }

    public City City1 { get; set; }
    public City City2 { get; set; }
}



public class City
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Customer> Customers1 { get; set; }
    public List<Customer> Customers2 { get; set; }
}

Next create Mapped class:

public class CustomerMap : IEntityTypeConfiguration<Customer>
{
    public void Configure(EntityTypeBuilder<Customer> builder)
    {
        builder.HasKey(k => k.Id);
        builder
            .HasOne(c => c.City1)
            .WithMany(c => c.Customers1);
        builder
            .HasOne(c => c.City2)
            .WithMany(c => c.Customers2).OnDelete(DeleteBehavior.Restrict);
    }
}

And for end override OnModelCreating method in DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.ApplyConfiguration(new CustomerMap());
}
mamad2559
  • 158
  • 2
  • 15