I need to access my db outside the typical .Application service (which uses injected IRepository objects). The table has been created with the migrator via the following:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TtmErrorLog",
columns: table => new
{
LogId = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
ErrorDate = table.Column<DateTime>(nullable: false),
CreateBy = table.Column<long>(nullable: true),
ErrorText = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_TtmErrorLog", x => x.LogId);
});
}
The context::
using System.Data.Entity;
namespace ExceptionsLibrary
{
public class ErrorLogContext : DbContext
{
public ErrorLogContext() : base("ngTTMDb")
{
}
public DbSet<ErrorLog> ErrorLogs { get; set; }
}
}
The table object:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ExceptionsLibrary
{
[Table("TtmErrorLog")]
public class ErrorLog
{
[Key]
public int LogId { get; set; }
public System.DateTime ErrorDate { get; set; }
public long? CreateBy { get; set; }
public string ErrorText { get; set; }
}
}
The ErrorWriter
library has the following:
public class ErrorWriter
{
private static ErrorLogContext errLogContext;
private ErrorLog errorItem;
private StringBuilder memoryString;
public void Init()
{
errLogContext = new ErrorLogContext();
errorItem = new ErrorLog;
}
public void writelog(int userId, string errmsg)
{
using (SqlConnection con = new SqlConnection(errLogContext.Database.Connection.ConnectionString))
{
errorItem.CreateBy = userId;
errorItem.ErrorText = errmsg;
errLogContext.ErrorLogs.Add(errorItem);
errLogContext.SaveChanges();
con.Close();
}
}
}
App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Installed packages:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net452" />
<package id="System.Data.Common" version="4.3.0" targetFramework="net452" />
<package id="System.Data.SqlClient" version="4.6.0" targetFramework="net452" />
</packages>
My app is Angular/Core. The code above has been migrated from another working app. I'm using the default MS-SQL Server that ANZ provides. The problem is an Exception is thrown when ' errLogContext.ErrorLogs.Add(errorItem);' is executed. The full Exception message is:
Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
How is the factory provided and registered?
4 Answer(s)
-
0
.NET Core does not support EF 6 yet.
-
0
@maliming - Is there another way to write to the db without being supplied the service's IRepository object? Can I obtain it locally? If so, how? Or must I pass the service's IRepository object down from the top?
-
0
Maybe you can try ADO.NET or Dapper directly.
-
0
I solved the issue by upgrading all my migrated code to .NetCore.