Share via

EFCORE Configuration

YAO ALLOU 20 Reputation points
2023-06-15T12:38:49.29+00:00
How to configure DataContext to record my 3 tables through Owner like that below.                                         using (var context = new YourDbContext())
{
    var newOwner = new Owner
    {
        OwnerName = "John Doe",
        Cars = new List<Car>
        {
            new Car
            {
                CarName = "Car 1",
                Images = new List<Image>
                {
                    new Image { ImageName = "Image 1" },
                    new Image { ImageName = "Image 2" }
                }
            },
            new Car
            {
                CarName = "Car 2",
                Images = new List<Image>
                {
                    new Image { ImageName = "Image 3" },
                    new Image { ImageName = "Image 4" }
                }
            }
        }
    };


Developer technologies | .NET | Entity Framework Core
0 comments No comments

Answer accepted by question author

Stephane PILLOY 100 Reputation points
2023-06-15T21:28:30.95+00:00

Hello,

this is how your DbContext should be

public class YourDbContext : DbContext 
{     
	public virtual DbSet<Owner> Owners { get; set; }
    public virtual DbSet<Car> Cars { get; set; }
    public virtual DbSet<Image> Images { get; set; }

	public YourDbContext(DbContextOptions<YourDbContext> context)
              : base(context)
    { 
	}

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {         
		modelBuilder.Entity<Owner>(o => 
        {            
			 o.ToTable("Owners"); 
            o.HasMany(owner => owner.Cars)
                 .WithOne(car => car.Owner)
                 .HasForeignKey(car => car.OwnerId);
        }); 
        modelBuilder.Entity<Car>(c =>
        {
            c.ToTable("Cars");
            c.HasMany(car => car.Images)
                .WithOne(image => image.Car)
                .HasForeignKey(image => image.CarId);
        });
        modelBuilder.Entity<Image>(i =>
        {
            i.ToTable("Images");
        });
    }
}

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2023-06-16T15:19:49.49+00:00

    First define your tables e.g. for SQL-Server in SSMS (SQL-Server Management Studio).

    Then test by inserting several records and querying them.

    Next install EF Core Power Tools for Visual Studio

    Use EF Power Tools as per the following instructions which will create models and configure relations. In EF Core Tools there is an option to include the connection string, do that and then for a production app move the connection string to perhaps appsettings.json.

    Note, if the tables change or columns change you can re-run EF Core Powers to pick-up the changes.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.