Extra orders table - how to

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Good day!
I want to make some extra information for orders. And I think to make extra table in DB with foreign key to Order.Id
Fetch 1:1. The scenario is - when I create a new order on this table should also create a new record, and delete scenario should be a cascade.
I'm curious how can I make this thing via plugin?
6 years ago
art_MOO wrote:
Good day!
I want to make some extra information for orders. And I think to make extra table in DB with foreign key to Order.Id
Fetch 1:1. The scenario is - when I create a new order on this table should also create a new record, and delete scenario should be a cascade.
I'm curious how can I make this thing via plugin?


I think you can use [dbo].[GenericAttribute] table for this.

But if want to make a new table from plugin you can. but note that you can't do foreign key relation bcz of different object context. You have access both table parallel. Both cases you have to delete any row   manually.
6 years ago
Subscribe to events Order Entity Inserted and Order Entity Deleted in your plugin code. Please find below is sample code.
public class MyPluiginClass : BasePlugin, IConsumer<EntityInserted<Order>>, IConsumer<EntityDeleted<Order>>
    {
        public void HandleEvent(EntityInserted<Order> eventMessage)
        {
            //TODO: Code to add records when Order is created
            throw new NotImplementedException();
        }

        public void HandleEvent(EntityDeleted<Order> eventMessage)
        {
            //TODO: Code to delete records when order is deleted
            throw new NotImplementedException();
        }
    }
6 years ago
you can create plugin and you can create new table to store extra order related data.

but you can not create foreign key as both is independent context.

you can handle OrderPlaced and OrderUpdate and OrderDeleted event handler to insert,update or delete the related records from new tables.
6 years ago
So ok I did it. But questions - in the future I want to add some more extra fields to this table. So how it will be? What I really need to do?
So, I add a new field in Entity. Then probably I need to make some Code First Migration right?
6 years ago
So I tried to update dbcontext like:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Plugin.Misc.CoreModelExtension.Core
{
    public static class DbContextExtensions
    {
        public static DbContext RefreshEntites(this DbContext dbContext, RefreshMode refreshMode, Type entityType = null)
        {
            var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
            var refreshableObjects = objectContext.ObjectStateManager
                .GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged)
                .Where(x => entityType == null)
                .Where(entry => entry.EntityKey != null)
                .Select(e => e.Entity)
                .ToArray();

            objectContext.Refresh(RefreshMode.StoreWins, refreshableObjects);

            return dbContext;
        }

        public static DbContext RefreshAllEntites(this DbContext dbContext, RefreshMode refreshMode)
        {
            return RefreshEntites(dbContext: dbContext, refreshMode: refreshMode, entityType: null); //null entityType is a wild card
        }

        public static DbContext RefreshEntites<TEntity>(this DbContext dbContext, RefreshMode refreshMode)
        {
            return RefreshEntites(dbContext: dbContext, refreshMode: refreshMode, entityType: typeof(TEntity));
        }
    }
}


But no result. Could u please help me with this question?
6 years ago
So I tried to update dbcontext like:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Plugin.Misc.CoreModelExtension.Core
{
    public static class DbContextExtensions
    {
        public static DbContext RefreshEntites(this DbContext dbContext, RefreshMode refreshMode, Type entityType = null)
        {
            var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
            var refreshableObjects = objectContext.ObjectStateManager
                .GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged)
                .Where(x => entityType == null)
                .Where(entry => entry.EntityKey != null)
                .Select(e => e.Entity)
                .ToArray();

            objectContext.Refresh(RefreshMode.StoreWins, refreshableObjects);

            return dbContext;
        }

        public static DbContext RefreshAllEntites(this DbContext dbContext, RefreshMode refreshMode)
        {
            return RefreshEntites(dbContext: dbContext, refreshMode: refreshMode, entityType: null); //null entityType is a wild card
        }

        public static DbContext RefreshEntites<TEntity>(this DbContext dbContext, RefreshMode refreshMode)
        {
            return RefreshEntites(dbContext: dbContext, refreshMode: refreshMode, entityType: typeof(TEntity));
        }
    }
}


But no result. Could u please help me with this question?
6 years ago
art_MOO wrote:
So ok I did it. But questions - in the future I want to add some more extra fields to this table. So how it will be? What I really need to do?
So, I add a new field in Entity. Then probably I need to make some Code First Migration right?


when you add new field to existing entity then you need to add new field in Entity and need to add manually on DB.

it will not automatically update the DB so it will be manual process for adding new field.

sorry but i am not sure that its possible to remigrate on existing entities to add or update fields
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.