InventoryManager / Data / InventoryContext.cs
InventoryContext.cs
Raw
using InventoryManager.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Options;
using System.Linq;

namespace InventoryManager.Data
{
    public class InventoryContext : IdentityDbContext
    {
        public InventoryContext(DbContextOptions<InventoryContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Unit> Units { get; set; }
        public virtual DbSet<Collections> Collections { get; set; }
        public virtual DbSet<Colors> Colors { get; set; }
        public virtual DbSet<Suppliers> Suppliers { get; set; }
        public virtual DbSet<Materials> Materials { get; set; }
        public virtual DbSet<Products> Products { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            foreach (var entity in builder.Model.GetEntityTypes())
            {
                var properties = entity.GetProperties().Where(p => p.ClrType == typeof(string));
                foreach (var property in properties)
                {
                    property.SetMaxLength(255);
                }
            }
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseMySQL("host=containers-us-west-129.railway.app;database=railway;uid=root;pwd=MEeCuvKIyj54HLztRa3U;port=6289;protocol=TCP");
            }
        }
    }
}