Answered by:
Web Form .net 4.8 EF custom connection error

Question
-
User-27405682 posted
Custom connection string for EF i hit error as below. im using .net 4.8 that not support for UseSqlServer. May i know what wrong am i,
optionsBuilder.UseSqlServer(connectionString);
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
public partial class S2GConnection : DbContext { public S2GConnection () { ConnUtility.INTDBConnStrEF(); } public virtual DbSet<portfolio> portfolios { get; set; } }
my ConnUtility.INTDBConnStrEF() is able to connect successful. That look no issues.
public static string INTDBConnStrEF() { ASCIIEncoding AE = new ASCIIEncoding(); decByteKey = AE.GetBytes(decByteText); // Initialize the EntityConnectionStringBuilder. EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(); try { // Specify the provider name, server and database. string providerName = "System.Data.SqlClient"; string serverName = ConfigurationManager.AppSettings["INTDBDataSource"]; string databaseName = ConfigurationManager.AppSettings["INTDB"]; string databaseUser = ConfigurationManager.AppSettings["INTDBUserID"]; string databasePass = ConfigurationManager.AppSettings["INTDBPassword"]; //string register = MyEncDec.Encrypt("", decKey, decByteKey); databasePass = MyEncDec.Decrypt(databasePass, decKey, decByteKey); // Initialize the connection string builder for the SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); // Set the properties for the data source. sqlBuilder.DataSource = serverName; sqlBuilder.InitialCatalog = databaseName; sqlBuilder.PersistSecurityInfo = true; sqlBuilder.Password = databasePass; sqlBuilder.UserID = databaseUser; // Build the SqlConnection connection string. string providerString = sqlBuilder.ToString(); //Set the provider name. entityBuilder.Provider = providerName; // Set the provider-specific connection string. entityBuilder.ProviderConnectionString = providerString; // Set the Metadata location. entityBuilder.Metadata = @"res://*/_DataAccess.Model.csdl|res://*/_DataAccess.Model.ssdl|res://*/_DataAccess.Model.msl"; using (EntityConnection conn = new EntityConnection(entityBuilder.ToString())) { conn.Open(); Console.WriteLine("Just testing the connection."); conn.Close(); } } catch (Exception ex) { LogUtility.LogException(ex.Message, "ConnUtility.INTDBConnStr"); } return Convert.ToString(entityBuilder); }
using (var context = new S2GConnection()) { var list = context.portfolios.Select(p => new { p.idDesc, p.category.tagCategory,p.category.tagColor, p.title, p.descWork, p.link, mainImg = p.ImgFolder + "/" + p.portfolioImg.imgMain, p.dateCreated }); return resultJSON = JsonConvert.SerializeObject(list); }
Why i try to open connection it will hit the above error. most internet solution is on .core.
Friday, December 25, 2020 7:09 AM
Answers
-
User753101303 posted
Hi,
You call a static method which returns a connection string that you don't use. For this kind of code I would expect the intent being to pass the connection string as part of constuctor? For example:
using (var context = new S2GConnection(ConnUtility.INTDBConnStrEF()))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 25, 2020 10:05 AM -
User-27405682 posted
Thanks you PatriceSc
Im using wrong dll should be using System.Data.Entity; rather than using Microsoft.EntityFrameworkCore;
my custom connection would be
public S2GConnection(): base(ConnUtility.INTDBConnStrEF()) {}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 25, 2020 2:34 PM
All replies
-
User753101303 posted
Hi,
You call a static method which returns a connection string that you don't use. For this kind of code I would expect the intent being to pass the connection string as part of constuctor? For example:
using (var context = new S2GConnection(ConnUtility.INTDBConnStrEF()))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 25, 2020 10:05 AM -
User-27405682 posted
Thanks you PatriceSc
Im using wrong dll should be using System.Data.Entity; rather than using Microsoft.EntityFrameworkCore;
my custom connection would be
public S2GConnection(): base(ConnUtility.INTDBConnStrEF()) {}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 25, 2020 2:34 PM