using Microsoft.EntityFrameworkCore;
public Guid Id { get; set; }
public string Title { get; set; }
public Guid CategoryId { get; set; }
public double OrderNumber { get; set; }
public int RX { get; set; }
public int RY { get; set; }
public class MyDbContext : DbContext
public MyDbContext(DbContextOptions<MyDbContext> options)
Database.EnsureCreated();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<Product> Products { get; set; }
private static Product CreateProduct(Guid categoryId,string title)
public static void Main()
var options = new DbContextOptionsBuilder<MyDbContext>()
.UseInMemoryDatabase(databaseName: "Test")
using (var context = new MyDbContext(options))
var business = new ProductBusiness(context);
var categoryId = Guid.NewGuid();
var firstProductId = business.Append(CreateProduct(categoryId,"first product"));
business.Append(CreateProduct(categoryId,"Second product"));
var thirdProductId = business.Prepend(CreateProduct(categoryId,"Third product"));
business.InsertAfter(firstProductId.Id,CreateProduct(categoryId,"forth product"));
business.InsertBefore(thirdProductId.Id,CreateProduct(categoryId,"fifth product"));
business.InsertBefore(firstProductId.Id,thirdProductId,false);
.OrderBy(x => x.OrderNumber)
Console.WriteLine(x.Title +" , " + x.RX.ToString() + " , " + x.RY.ToString() );
public class ProductBusiness
private MyDbContext _dbContext;
private decimal insertUnit = Decimal.Parse(".0");
private const decimal appendUnit = 1;
public ProductBusiness(MyDbContext dbContext)
this._dbContext = dbContext;
public Product InsertAfter(Guid id,Product product,bool insert=true)
var previousProduct = _dbContext.Products
.Where(x => x.CategoryId == product.CategoryId && x.Id==id)
if(previousProduct == null)
throw new Exception("Product not found");
var nextProduct = _dbContext.Products
.Where(x => x.CategoryId == product.CategoryId && x.OrderNumber >= previousProduct.OrderNumber)
.OrderBy(x => x.OrderNumber)
var rx = previousProduct.RX + (nextProduct!= null?nextProduct.RX : 1);
var ry = nextProduct!= null ? nextProduct.RY + previousProduct.RY : 1;
var newOrderNumber = rx/ry;
product.OrderNumber = newOrderNumber;
_dbContext.Add<Product>(product);
_dbContext.SaveChanges();
public Product InsertBefore(Guid id,Product product,bool insert=true)
var nextProduct = _dbContext.Products
.Where(x => x.CategoryId == product.CategoryId && x.Id==id)
throw new Exception("Product not found");
var prevProduct = _dbContext.Products
.Where(x => x.CategoryId == product.CategoryId && x.OrderNumber <= nextProduct.OrderNumber)
.OrderByDescending(x => x.OrderNumber)
var rx = nextProduct.RX + (prevProduct!= null ? prevProduct.RX : -1);
var ry = prevProduct != null ? (prevProduct.RY+nextProduct.RY) : 1;
var newOrderNumber = rx/ry;
product.OrderNumber = newOrderNumber;
_dbContext.Add<Product>(product);
_dbContext.SaveChanges();
public Product Append(Product product)
var lastProduct = _dbContext.Products
.Where(x => x.CategoryId == product.CategoryId)
.OrderByDescending(x => x.OrderNumber)
var rx = lastProduct != null ? lastProduct.RX+1 : 1;
var newOrderNumber = rx/ry;
product.OrderNumber = newOrderNumber;
_dbContext.Add<Product>(product);
_dbContext.SaveChanges();
public Product Prepend(Product product)
var lastProduct = _dbContext.Products
.Where(x => x.CategoryId == product.CategoryId)
.OrderBy(x => x.OrderNumber)
var rx = lastProduct != null ? lastProduct.RX-1 : 1;
var newOrderNumber = rx/ry;
product.OrderNumber = newOrderNumber;
_dbContext.Add<Product>(product);
_dbContext.SaveChanges();
public void Remove(Product product)
_dbContext.Remove<Product>(product);
_dbContext.SaveChanges();