Evilripper
Evilripper

Reputation: 1185

ConnectionString Builder in Entity Framework Core 2

I'm trying to convert an old project c# to asp net core and to the EF core 2.0 and I can't find the EntityConnectionStringBuilder.

public string GetEntityConnectionString(string databaseName, string metadata)
{
    string providerName = "System.Data.SqlClient";
    string providerString = GetConnectionString(databaseName);


    System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder entityBuilder =
    new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder();

Error CS0234 The type or namespace name 'Entity' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

Upvotes: 3

Views: 4887

Answers (2)

Evk
Evk

Reputation: 101473

That's because EF Core does not support what was known "database first" approach of older EF versions, so there are no edmx files, and so there is no use for EF specific connection strings (metadata=res:... < this kind of connection strings). From that it follows that there is no use for EntityConnectionStringBuilder class, which is used to create such connection strings.

So when porting, just remove your whole GetEntityConnectionString method and use your GetConnectionString instead, together with UseSqlServer, UsePgsql and similar methods.

Upvotes: 2

Igorgy
Igorgy

Reputation: 170

You may set connection on Context. Try override OnConfiguring method:

    public class CommonDbContext : DbContext
    {
        public CommonDbContext(DbContextOptions<CommonDbContext> options)
            : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Data Source=127.0.0.1;Initial Catalog=DBName;User ID=user;Password=pwd;Connection Timeout=60");
        }
    }

Upvotes: 0

Related Questions