最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Entity Framework Core code-first connection string for multiple repositories - Stack Overflow

programmeradmin0浏览0评论

I have my own framework that I've been working on for years. It has an AppSecurity project that handles users, roles, permissions, etc. For it, I created an EF Core code-first DbContext class:

internal class AppSecurityDataContext : DbContext
{
    private string _connectionString = @"Server=mysite;Database=TheLastAppIWorkedOn;User Id=abc1;Password=xyx2;";

    public AppSecurityDataContext()
    {
    }

    public AppSecurityDataContext(string connectionString)
    {
        _connectionString = connectionString;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(_connectionString);
    }

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

        modelBuilder.Entity<UserEntity>().HasData
        (
            new UserEntity
            {
                // Properties set here
            }
        );
    }
}

Right now I'm working on a WPF app called Falcon. It has its own DbContext which manages the Falcon DB objects. Right now, the connection string for it is stored in the DbContext class becuase I'm not sure how this is supposed to be done.

private string _connectionString = @"Server=mysite;Database=Falcon;User Id=someone;Password=somepwd;";

public FalconDataContext()
{
}

public FalconDataContext(string connectionString)
{
    _connectionString = connectionString;
}

public FalconDataContext(DbContextOptions<FalconDataContext> options) :
    base(options)
{
}

Next week, I could be in another app that needs to use AppSecurity. Herein lies the problem - when I work on other apps that use the AppSecurity project, I have to open its DbContext class, change the connection string, then open Package Manager and rebuild.

So, how can my AppSecurity project get the connection string the other project I'm working?

发布评论

评论列表(0)

  1. 暂无评论