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

I have my own framework that I've been working on for years. It has an AppSecurity project that ha

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742385286a4433932.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信