繁体   English   中英

实体框架选择 DBContext

[英]Entity Framework pick DBContext

我有一个使用 EF Core 6 和 Azure SQL 的 .NET 6 项目,其中有两个DbContext ,一个用于我们在(欧盟、英国)运营的每个区域。

我希望能够在时间或运行时为该区域选择DbContext

这是我所拥有的,它可以工作,但是很麻烦,

if (loc == "UK")
{
    ContextUK db = new ContextUK();

    var subs = db.Subscriptions.Where(s => s.Status == "Active" )
                               .Select(s => s.Id);

    return db.Addons.Where(s => subs
                            .Contains(s.Id))
                            .ToList();
   }
   else
   {
      Context db = new Context();

      var subs = db.Subscriptions.Where(s => s.Status == "Active")
                                 .Select(s => s.Id);

      return db.Addons.Where(s => subs
                              .Contains(s.Id))
                              .ToList();
   }

我试过这个

var db = loc == "UK" ? new ContextUK() : new Context();

但是我得到一个隐式转换错误,有什么想法吗?

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Poller.Models;

namespace Poller
{
    public partial class Context : DbContext
    {
        public Context()
        {
        }

        public Context(DbContextOptions<Context> options)
            : base(options)
        {
        }
        public virtual DbSet<Reseller> Resellers { get; set; } = null!;

        public virtual DbSet<dBCustomers> Customers { get; set; } = null!;

        public virtual DbSet<dBProduct> Products { get; set; } = null!;

        public virtual DbSet<dBSubscription> Subscriptions { get; set; } = null!;

        public virtual DbSet<dBAddons> Addons { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                
                SqlConnectionStringBuilder builder = new()
                {
                    ////
                };

                optionsBuilder
                .UseSqlServer(builder.ConnectionString);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            modelBuilder.Entity<dBCustomer>(entity =>
            {
                entity.HasKey(e => e.CustomerId)
                    .HasName("PK_Customers_CustomerId");

                entity.Property(e => e.CustomerId).HasMaxLength(64);

                entity.Property(e => e.CustomerName).HasMaxLength(512);

            });

            modelBuilder.Entity<dBSubscription>(entity =>
            {
                entity.ToTable(tb => tb.IsTemporal(ttb =>
                {
                    ttb.UseHistoryTable("subscriptions", "His");
                    ttb
                        .HasPeriodStart("SysStartTime")
                        .HasColumnName("SysStartTime");
                    ttb
                        .HasPeriodEnd("SysEndTime")
                        .HasColumnName("SysEndTime");
                }));
                
                entity.HasIndex(e => e.CustomerId, "nci_wi_Subscriptions_1A4A4BFC7994C478D5C6262FFAD2CF90");

                entity.HasIndex(e => e.Reseller, "nci_wi_Subscriptions_AFF865B2B84F59CEDC317D93B1DAE852");

                entity.Property(e => e.Id).HasMaxLength(64);

                entity.Property(e => e.CommitmentEndDate).HasColumnType("date");

                entity.Property(e => e.EffectiveStartDate).HasColumnType("date");

                entity.Property(e => e.OfferId).HasMaxLength(64);

                entity.Property(e => e.OfferName).HasMaxLength(256);

                entity.Property(e => e.ProductType).HasMaxLength(16);

                entity.Property(e => e.Reseller)
                    .HasMaxLength(16)
                    .HasColumnName("Reseller");

                entity.Property(e => e.Status).HasMaxLength(16);

                entity.Property(e => e.CustomerId).HasMaxLength(64);

                entity.Property(e => e.TermDuration)
                    .HasMaxLength(16)
                    .HasColumnName("termDuration");

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.Subscriptions)
                    .HasForeignKey(d => d.CustomerId)
                    .HasConstraintName("FK_Customers_CustomerId");
            });

            modelBuilder.Entity<dBProduct>(entity =>
            {
                entity.HasNoKey();

                entity.Property(e => e.Title).HasMaxLength(512);

                entity.Property(e => e.Term).HasMaxLength(512);

                entity.Property(e => e.Billing).HasMaxLength(512);

                entity.Property(e => e.Type).HasMaxLength(512);
            });

            modelBuilder.Entity<Reseller>(entity =>
            {
                entity.HasKey(e => e.Reseller)
                    .HasName("PK__Reseller__054B69376F9BA262");

                entity.Property(e => e.Reseller)
                    .HasMaxLength(32)
                    .HasColumnName("Reseller");

                entity.Property(e => e.ResellerName).HasMaxLength(512);
            });

            modelBuilder.Entity<dBaddons>(entity =>
            {
                entity.HasKey(e => e.addonsId)
                    .HasName("PK__addons__411A3D16E3765B5F");

                entity.Property(e => e.addonsId).HasMaxLength(64);

                entity.Property(e => e.Id).HasMaxLength(64);

                entity.Property(e => e.Status).HasMaxLength(16);

                entity.Property(e => e.CustomerId).HasMaxLength(64);

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.addonss)
                    .HasForeignKey(d => d.CustomerId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK__addons__cust__2759D01A");
            });
        }
    }
}

我通过创建一个接口然后将其添加到两个 dbcontexts 解决了这个问题,

界面:

public interface IDatabaseContext
{
    DbSet<Reseller> Resellers { get; set; }

    DbSet<dBCustomers> Customers { get; set; }

    DbSet<dBProduct> Products { get; set; }

    DbSet<dBSubscription> Subscriptions { get; set; }

    DbSet<dBAddons> Addons { get; set; }
}

数据库上下文:

public partial class Context : DbContext, IDatabaseContext

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM