DataConnector | ComponentOne
ADO.NET provider for Magento / Entity Framework
In This Topic
    Entity Framework
    In This Topic

    The ADO.NET Provider for Magento supports Entity Framework which requires C1.EntityFrameworkCore.Magento package to be installed. In this section, the model first approach is used to build an Entity Framework model that maps data tables to classes for simpler access to Magento.

    The following code defines the Products class to map the Products datatable.

    C#
    Copy Code
    public partial class Products
        {
            public int Id { get; set; }
            public int attribute_set_id { get; set; }
            public DateTime? created_at { get; set; }
            public string media_gallery_entries { get; set; }
            public string Name { get; set; }
            public string Options { get; set; }
            public string Price { get; set; }
            public string product_links { get; set; }
            public string Sku { get; set; }
            public int Status { get; set; }
            public string tier_prices { get; set; }
            public string type_id { get; set; }
            public DateTime? updated_at { get; set; }
            public int Visibility { get; set; }
            public string Weight { get; set; }
            public string custom_attributes { get; set; }
        }

    The next code example defines MagentoContext class to access the Products datatable and establish a connection to the Magento service by overriding the OnConfiguring method. This method invokes the UseMagento method of the DbContextOptionsBuilder class to configure the context and establish a connection with the Magento service.

    C#
    Copy Code
        public partial class MagentoContext : DbContext
        {
            public MagentoContext()
            {
                Database.AutoTransactionsEnabled = false;
            }
    
            public MagentoContext(DbContextOptions options)
                : base(options)
            {
                Database.AutoTransactionsEnabled = false;
            }
            public virtual DbSet Products { get; set; }
            public virtual DbSet CustomerGroups { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseMagento($"Username='*****';Password='*****';Url='*****';Token Type='*****'");
                }
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity(entity =>
                {
                    entity.Property(e => e.Id).IsRequired();
    
                    entity.Property(e => e.attribute_set_id).ValueGeneratedOnAddOrUpdate();
    
                    entity.Property(e => e.media_gallery_entries).ValueGeneratedOnAddOrUpdate();
    
                    entity.Property(e => e.created_at).ValueGeneratedOnAddOrUpdate();             
    
                    entity.Property(e => e.Options).ValueGeneratedOnAddOrUpdate();
                });
    
                modelBuilder.Entity(entity =>
                {
                    entity.Property(e => e.Id).IsRequired();
    
                    entity.Property(e => e.tax_class_id).ValueGeneratedOnAddOrUpdate();
    
                    entity.Property(e => e.Code).ValueGeneratedOnAddOrUpdate();
                    entity.Property(e => e.tax_class_name).ValueGeneratedOnAddOrUpdate();
                    entity.Property(e => e.extension_attributes).ValueGeneratedOnAddOrUpdate();
                    
                });        
    
                OnModelCreatingPartial(modelBuilder);
            }
    
            partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
        }

    You can now use the LINQ queries to perform different data operations to the mapped classes as demonstrated in the code below.

    Note: For LINQ queries,  "using System.Linq" must be declared in the code.
    C#
    Copy Code
    public void Select()
    {
        Console.WriteLine("Query all Products...");
        using (var context = new MagentoContext())
        {
            foreach (var product in context.Products)
            {
                Console.WriteLine($"{product.Id} - {product.Name} - {product.Sku} - {product.Price}");
            }
        }
    }
    Note: The Scaffolding feature supports the user to create the model and dbcontext when you create a model in the Entity Framework for all dataconnectors.