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" }
                }
            }
        }
    };


Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
773 questions
0 comments No comments
{count} votes

Accepted answer
  1. 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");
            });
        }
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,476 Reputation points
    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.

    0 comments No comments

Your answer

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