I'm currently building a .NET core Identity Server application that uses migrations and wondered what the best method of seeding a database would be in regards to .NET core.
Currently I have a DbInitializer class that is called on startup (see below) but wondered if I should be doing this as a migration?
public static void Seed(IApplicationBuilder applicationBuilder)
{
ConfigurationDbContext context =
applicationBuilder.ApplicationServices.GetRequiredService<ConfigurationDbContext>();
IConfiguration oConfig =
applicationBuilder.ApplicationServices.GetRequiredService<IConfiguration>();
string sSeedingConfig = GetSeedingConfiguration(oConfig);
SeedConfigurationDb(ref context, sSeedingConfig);
}
private static void SeedConfigurationDb(ref ConfigurationDbContext oContext, string sSeedingConfig)
{
if (!oContext.ApiResources.Any())
{
var oApis = GetSeedingConfigElements(sSeedingConfig, "Apis");
List<ApiResource> lApis = null;
if (oApis != null)
{
lApis = JsonConvert.DeserializeObject<List<ApiResource>>(oApis.ToString());
}
if (lApis != null)
{
foreach (var api in lApis)
{
oContext.ApiResources.Add(api.ToEntity());
}
}
}
if (!oContext.Clients.Any())
{
var oClients = GetSeedingConfigElements(sSeedingConfig, "Clients");
List<Client> lClients = null;
if (oClients != null)
{
lClients = JsonConvert.DeserializeObject<List<Client>>(oClients.ToString());
}
if (lClients != null)
{
foreach (var client in lClients)
{
oContext.Clients.Add(client.ToEntity());
}
}
}
if (!oContext.IdentityResources.Any())
{
var oIdentityResources = GetSeedingConfigElements(sSeedingConfig, "IdentityResources");
List<IdentityResource> lIdentityResources = null;
if (oIdentityResources != null)
{
lIdentityResources = JsonConvert.DeserializeObject<List<IdentityResource>>(oIdentityResources.ToString());
}
if (lIdentityResources != null)
{
foreach (var identityresource in lIdentityResources)
{
oContext.IdentityResources.Add(identityresource.ToEntity());
}
}
}
oContext.SaveChanges();
}
When you enable your migrations, you get a class file named "Configuration". This class has a seed method that gets run every time you update the database trough a migration.
So you could seed your initial update trough there, and then comment it out.
Related
Actually, I am trying to write an open-source project called Telemetrium. It is a baby step but I got stuck with something: I will explain the situation in 2 parts
Without Telemetrium
With Telemetrium
Without Telemetrium:
Files :
Function1.cs
Startup.cs
TelemetryInitializer.cs
Everything is working good like below:
TelemetryInitializer.cs:
public class TelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor _httpContextAccessor;
public TelemetryInitializer(IHttpContextAccessor httpContextAccessor)
{
if (httpContextAccessor == null) throw new System.ArgumentNullException(nameof(httpContextAccessor));
_httpContextAccessor = httpContextAccessor;
}
public async void Initialize(ITelemetry telemetry)
{
if (telemetry is DependencyTelemetry)
{
var dependencyTelemetry = telemetry as DependencyTelemetry;
if (dependencyTelemetry.Success == true)
dependencyTelemetry = dependencyTelemetry.ActivityToTelemetry(System.Diagnostics.Activity.Current) as DependencyTelemetry;
dependencyTelemetry.Success = true;
}
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
var context = _httpContextAccessor.HttpContext;
if (context == null) return;
try
{
if (context.Request != null)
{
requestTelemetry = requestTelemetry.ActivityToTelemetry(System.Diagnostics.Activity.Current) as RequestTelemetry;
context.Request.EnableBuffering();
context.Request.Body.Position = 0;
requestTelemetry.Properties[$"Request"] = await new StreamReader(context.Request.Body).ReadToEndAsync();
foreach (var headerName in context.Request.Headers)
{
var header = context.Request.Headers[headerName.Key];
requestTelemetry.Properties[$"Request-{headerName.Key}"] = header;
}
}
if (context.Response != null && (context.Response.Body.CanRead && context.Response.Body.CanSeek))
{
requestTelemetry.Properties[$"Response"] = await new StreamReader(context.Response.Body).ReadToEndAsync();
foreach (var headerName in context.Response.Headers)
{
var header = context.Response.Headers[headerName.Key];
requestTelemetry.Properties[$"Response-{headerName.Key}"] = header;
}
}
}
catch (ObjectDisposedException ex)
{
Console.WriteLine($"obj accessed after dispose!: {ex.Message}");
Debug.WriteLine($"obj accessed after dispose!: {ex.Message}");
requestTelemetry.Success = false;
}
requestTelemetry.Success = true;
}
}
public static class Extensions
{
public static ITelemetry ActivityToTelemetry(this OperationTelemetry telemetry, Activity activity)
{
if (telemetry == null)
throw new ArgumentNullException(nameof(telemetry));
if (activity.Tags.Count() > 0)
foreach (var tag in activity.Tags)
telemetry.Properties[tag.Key] = tag.Value;
telemetry.Properties["Id"] = activity.Id;
telemetry.Properties["ParentId"] = activity.ParentId;
telemetry.Properties["RootId"] = activity.RootId;
return telemetry;
}
}
But I decided to make some changes for Telemetrium:
What I did:
I added Attributes
I developed reflection based Extension
This is Telemetrium.cs:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class TelemetriumAttribute : Attribute
{
private TelemetryEnum TelemetryType;
private string MethodName;
public TelemetriumAttribute(TelemetryEnum TelemetryType, string MethodName)
{
this.TelemetryType = TelemetryType;
this.MethodName = MethodName;
}
public void RUN()
{
if (TelemetryType == TelemetryEnum.RequestTelemtry)
{
var req = Telemetrium.telemetryClient.StartOperation<RequestTelemetry>(this.MethodName);
}
else if (TelemetryType == TelemetryEnum.DependencyTelemetry)
{
var dep = Telemetrium.telemetryClient.StartOperation<DependencyTelemetry>(this.MethodName);
}
}
}
public enum TelemetryEnum
{
RequestTelemtry, DependencyTelemetry
}
public static class Telemetrium
{
public static TelemetryClient telemetryClient { get; set; }
static public void StartTelemetrium(this Function1 This)
{
Type objType = typeof(Function1);
try
{
MethodInfo[] info = objType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
var methods = objType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(m => m.GetCustomAttributes(typeof(TelemetriumAttribute), false).Length > 0).ToArray();
var customAttributes = objType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(m => m.GetCustomAttributes(typeof(TelemetriumAttribute), false).Length > 0)
.Select(q => q.GetCustomAttributes(typeof(TelemetriumAttribute), false)).ToArray();
if (customAttributes.Length > 0 && customAttributes[0] != null)
{
foreach (var customAttribute in customAttributes)
{
foreach (var item in customAttribute)
{
TelemetriumAttribute _customAttribute = item as TelemetriumAttribute;
_customAttribute.RUN();
}
}
}
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
By using Telemtrium I want to Convert method1 in Function1.cs from
private void method1()
{
using (var dep1 = Telemetrium.telemetryClient.StartOperation<DependencyTelemetry>("method1"))
{
int a = 2;
int b = 2;
int v = a * b;
dep2.Telemetry.Success = true;
}
}
TO
[Telemetrium(TelemetryEnum.DependencyTelemetry, "method1")]
private void method1()
{
//using (var dep1 = telemetryClient.StartOperation<DependencyTelemetry>("method1"))
//{
int a = 2;
int b = 2;
int v = a * b;
//dep1.Telemetry.Success = true;
//}
}
As a Result:
I don`t want to use like that
using (var getDetailsOperation = Telemetrium.telemetryClient.StartOperation("GetProductDetails"))
When any function in azure it should be automatically can in RUN method.
also when submethods run, using (var dep2 = Telemetrium.telemetryClient.StartOperation("method2")) should be created automatically. But How?
I use following method to update orgnrs in a database:
UpdateService service = new UpdateService()
service.UpdateDocsA(orgnrs);
public void UpdateDocsA(List<string> orgnrs)
{
using (var context = new Data.InexchangeEntitiesA())
{
foreach (string orgnr in orgnrs)
{
try
{
Data.Customers customer = new Data.Customers();
customer.CustNo = orgnr.Trim();
context.Customers.Add(customer);
}
catch (Exception ex)
{
}
}
context.SaveChanges();
}
}
Problem is that I have multiple databases with similar update.
service.UpdateDocsA(orgnrs);
service.UpdateDocsB(orgnrs);
service.UpdateDocsC(orgnrs);
service.UpdateDocsC(orgnrs);
The only difference is in following line:
using (var context = new Data.InexchangeEntitiesA())
using (var context = new Data.InexchangeEntitiesB())
using (var context = new Data.InexchangeEntitiesC())
using (var context = new Data.InexchangeEntitiesD())
I want to create a generic update method. Any ideas how can I achieve it? or how to pass Data.InexchangeEntities to a method?
Assuming that the method signature for InexchangeEntitiesA() and InexchangeEntitiesB() etc. is common why not pass that in to your UpdateDocs method?
If we assume those methods all return an IDataContext object which implements a Customers() method;
UpdateService service = new UpdateService()
service.UpdateDocs(Data.InexchangeEntitiesA, orgnrs);
service.UpdateDocs(Data.InexchangeEntitiesB, orgnrs);
service.UpdateDocs(Data.InexchangeEntitiesC, orgnrs);
service.UpdateDocs(Data.InexchangeEntitiesD, orgnrs);
public void UpdateDocs<T>(Func<T> getContext, List<string> orgnrs) where T : IDataContext
{
using (var context = getContext())
{
foreach (string orgnr in orgnrs)
{
try
{
Data.Customers customer = context.Customers();
customer.CustNo = orgnr.Trim();
context.Customers.Add(customer);
}
catch (Exception ex)
{
}
}
context.SaveChanges();
}
}
Or something like this
public class Customer { }
public interface ISomeFunkyInterface
{
DbSet<Customer> Customers { get; set; }
}
public class DbContextA : DbContext, ISomeFunkyInterface
{
public DbSet<Customer> Customers { get; set; }
}
public void UpdateDocs<T>(List<string> orgnrs)
where T : DbContext, ISomeFunkyInterface ,new()
{
using var context = new T();
foreach (string orgnr in orgnrs)
{
context.Customers.Add(...);
}
context.SaveChanges();
}
service.UpdateDocs(orgnrs, new Data.InexchangeEntitiesA());
service.UpdateDocs(orgnrs, new Data.InexchangeEntitiesB());
service.UpdateDocs(orgnrs, new Data.InexchangeEntitiesC());
service.UpdateDocs(orgnrs, new Data.InexchangeEntitiesD());
service.UpdateDocs(orgnrs, new Data.InexchangeEntitiesE());
...
public void UpdateDocs(List<string> orgnrs, YOURCONTEXTTYPE context)
{
using (context)
{
foreach (string orgnr in orgnrs)
{
try
{
Data.Customers customer = new Data.Customers();
customer.CustNo = orgnr.Trim();
context.Customers.Add(customer);
}
catch (Exception ex)
{
}
}
context.SaveChanges();
}
}
There are multiple ways to achieve that. The most obvious to me is extract what is common and create multiple methods for that:
public void UpdateDocsA(List<string> orgnrs)
{
using (var context = new Data.InexchangeEntitiesA())
{
Perform(context, orgnrs);
}
}
void Perform(DbContext context, List<string> orgnrs)
{
foreach (string orgnr in orgnrs)
{
try
{
Data.Customers customer = new Data.Customers();
customer.CustNo = orgnr.Trim();
context.Customers.Add(customer);
}
catch (Exception ex)
{ }
}
context.SaveChanges();
}
This will reduce the duplicated code massivly. However it still feels ugly, because you still have four Update-methods.
You could also extract what is uncommon and inject a paremeter to switch on:
public void UpdateDocs(string type, List<string> orgnrs)
{
using (var context = GetContext(type))
{
Perform(context, orgnrs);
}
}
DbContext GetContext(string type)
{
return type == "A" ? new Data.InexchangeEntitiesA() :
type == "B" ? new Data.InexchangeEntitiesB() :
type == "C" ? new Data.InexchangeEntitiesC() :
new Data.InexchangeEntitiesD();
}
This does not seem much better, does it? We only have a single method, however it still uses some weird type-switching. Let´s try something more generic - assuming all your entities have some common base-interface or -class:
public void UpdateDocs<T>(List<string> orgnrs) where T: IMyInterface, new()
{
using (var context = new T)
{
Perform(context, orgnrs);
}
}
I'm creating an app that should work with various sqlite databases (with same structure) and I want to be able to open and close different databases. I use EF6 and I just can't figure out how to open a different database file (and make EF to reload and use data from new file). There are many questions regarding this but none of them work for me.
this is my dbContext generated by EF
public partial class mainEntities : DbContext
{
public mainEntities()
: base("name=mainEntities")
{
}
...
}
this is how I use and try to update my context
class Db
{
private static mainEntities myDbInstance;
public static mainEntities MyDbInstance
{
get
{
if (myDbInstance == null)
{
myDbInstance = new mainEntities();
}
return myDbInstance;
}
}
public static void UpdateConnectionString(string pth)
{
StringBuilder sb = new StringBuilder();
sb.Append("metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;");
sb.Append("provider=System.Data.SQLite.EF6;");
sb.Append("provider connection string=");
sb.Append("'");
sb.Append("datetime format=JulianDay;");
sb.Append("foreign keys=True;");
sb.Append("data source=");
sb.Append(pth);
sb.Append("'");
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["mainEntities"].ConnectionString = sb.ToString();
connectionStringsSection.ConnectionStrings["mainEntities"].ProviderName = "System.Data.EntityClient";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();
//reset context - does not work
MyDbInstance.Dispose();
myDbInstance = null;
}
}
I am able to update the connectionString,
var conn_str = System.Configuration.ConfigurationManager.ConnectionStrings["mainEntities"].ConnectionString;
returns correct string after I change database file, but data is loaded from the original one.
EDIT:
I check if the connection changed with simple WPF GUI
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void refresh_labels()
{
var conn_str = System.Configuration.ConfigurationManager.ConnectionStrings["mainEntities"].ConnectionString;
var rows_count =
(from d in Db.MyDbInstance.TagsFiles
select d).Count();
label1.Content = conn_str.Split(';')[4];
label2.Content = rows_count;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
String DbFileName1 = #"c:\tmp\db1.db3";
String DbFileName2 = #"c:\tmp\db2.db3";
var conn_str = System.Configuration.ConfigurationManager.ConnectionStrings["mainEntities"].ConnectionString;
if (conn_str.Contains(DbFileName1))
{
Db.UpdateConnectionString(DbFileName2);
}
else
{
Db.UpdateConnectionString(DbFileName1);
}
refresh_labels();
}
}
I manually created a class
public class AddClientsTable : DbMigration, IMigrationMetadata
{
string IMigrationMetadata.Id
{
get { return "201611281757258_AddClientsTable"; }
}
string IMigrationMetadata.Source
{
get { return null; }
}
string IMigrationMetadata.Target
{
get { return "AddClientsTable-Migration"; }
}
public override void Up() {
CreateTable("Clients", t => new {
ClientId = t.Guid(name:"ClientId"),
Name = t.String()
})
.PrimaryKey( t => t.ClientId, "ClientId")
.Index( t => t.ClientId, "PK_Clients", true);
}
public override void Down() {
DropIndex("Clients", "PK_Clients");
DropTable("Clients");
}
}
and i want to apply it via code-first migrations from code like this :
var migration = new AddClientsTable();
migration.Up();
context.RunMigration(migration);
which I stole from here but when I run the code I'm getting this exception :
Unable to cast object of type 'System.Data.Entity.Migrations.Model.CreateIndexOperation' to type 'System.Data.Entity.Migrations.Model.HistoryOperation'.
HistoryOperation is the operation which updates __MigrationHistory table ? so How do I do that via code ?
Am I missing something or the EntityFrameowrk Update-Database command does more than what I'm aware of ?
It doesn't make sense to cherry pick a migration and run it, because the migrations are cumulative and must be run in sequence. As such, you'd be better to run the equivalent of update-database powershell command at application startup.
Here's some code we use to do that:
In the Configuration.cs class constructor (this file was made when you enable-migrations)
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
then at app startup call the following method:
public static void ApplyDatabaseMigrations()
{
//Configuration is the class created by Enable-Migrations
DbMigrationsConfiguration dbMgConfig = new Configuration()
{
ContextType = typeof(MyDbContext) //+++++CHANGE ME+++++
};
using (var databaseContext = new MyDbContext()) //+++++CHANGE ME+++++
{
try
{
var database = databaseContext.Database;
var migrationConfiguration = dbMgConfig;
migrationConfiguration.TargetDatabase =
new DbConnectionInfo(database.Connection.ConnectionString,
"System.Data.SqlClient");
var migrator = new DbMigrator(migrationConfiguration);
migrator.Update();
}
catch (AutomaticDataLossException adle)
{
dbMgConfig.AutomaticMigrationDataLossAllowed = true;
var mg = new DbMigrator(dbMgConfig);
var scriptor = new MigratorScriptingDecorator(mg);
string script = scriptor.ScriptUpdate(null, null);
throw new Exception(adle.Message + " : " + script);
}
}
}
I am debugging an asp.net application. It works flawlessly in test environment, however, I am getting null reference exception in real server. I am using ninject. The problem arose from ninject I suppose.Here is the problematic code fragment:
public partial class PersonelAylikRapor : MasterClass
{
protected void btnSorgula_Click(object sender, EventArgs e)
{
//other codes ommited for brevity
DateTime baslangicTarihi = DateTime.MinValue;
//arac is null here.
baslangicTarihi = this.arac.CevirDateTimea("01." + ddlAy.SelectedValue + "." + ddlYil.SelectedValue);
}
}
the variable arac should have been solved in MasterClass, since it is the father class, so I do not check null reference issue.
MasterClass is the place that I set the ninject kernel. Here is the content of the MasterClass:
public class MasterClass : System.Web.UI.Page
{
IKernel ninjectKernel = null;
private bool cekirdekKurulduMu = false;
public IPersonelIsKurali personelik = null;
public IAraclarTaha arac = null;
public ITurnikePersonelIsKurali turnikepersonelik = null;
public IPersonelBirimlerIsKurali personelbirimlerik = null;
public ITurnikeIslemlerIsKurali turnikeIsKurali = null;
public IPersonelIliskilendir personelilisiklendirik = null;
public IBirimlerIsKurali birimlerik = null;
public IPersonelIzinIsKurali personelizinik = null;
public IServisIsKurali servisIsKurali = null;
public FonksiyonSonuc fs = null;
public List<PersonelKunye> listpersonelkunye = null;
public List<uint> listgorebilecegipersonelid = null;
public MasterClass()
{
}
protected override void OnPreInit(EventArgs e)
{
try
{
base.OnPreInit(e);
if (this.cekirdekKurulduMu == false)
{
this.cekirdekKurulduMu = true;
this.ninjectKernel = new StandardKernel();
this.ninjectCekirdegiKur(this.ninjectKernel);
this.DegiskenlereAta();
}
}
catch (Exception ex)
{
IAraclarTaha arac = new AraclarTaha();
FonksiyonSonuc fs = new FonksiyonSonuc(true);
fs = arac.HatayiVeritabaninaYaz(ex, OrmanSuTypes.Enums.HataCiddiyetiEnum.OLUMCUL);
if (fs.fonksiyonBasariDurumu == false)
{
throw ex;
}
}
}
private void ninjectCekirdegiKur(IKernel ninjectKernel)
{
this.ninjectKernel = new StandardKernel();
this.ninjectKernel.Bind<IPersonelBirimlerIsKurali>().To<PersonelBirimlerIsKurali>();
this.ninjectKernel.Bind<IPersonelIzinIsKurali>().To<PersonelIzinIsKurali>();
this.ninjectKernel.Bind<IPersonelIsKurali>().To<PersonelIsKurali>();
this.ninjectKernel.Bind<IAraclarTaha>().To<AraclarTaha>().WithConstructorArgument("debugMode", Araclar.DebugModdaMi());
this.ninjectKernel.Bind<ITurnikeIslemlerIsKurali>().To<TurnikeIslemlerIsKurali>();
this.ninjectKernel.Bind<IBirimlerIsKurali>().To<BirimlerIsKurali>();
this.ninjectKernel.Bind<IPersonelIliskilendir>().To<PersonelIiskilendirIsKurali>();
this.ninjectKernel.Bind<ITurnikePersonelIsKurali>().To<TurnikePersonelIsKurali>();
this.ninjectKernel.Bind<IServisIsKurali>().To<ServisIsKurali>();
}
public void DegiskenlereAta()
{
if (this.arac == null)
{
this.arac = this.ninjectKernel.Get<IAraclarTaha>();
}
if (this.personelik == null)
{
this.personelik = this.ninjectKernel.Get<IPersonelIsKurali>();
}
if (this.turnikepersonelik == null)
{
this.turnikepersonelik = this.ninjectKernel.Get<ITurnikePersonelIsKurali>();
}
if (this.personelbirimlerik == null)
{
this.personelbirimlerik = this.ninjectKernel.Get<IPersonelBirimlerIsKurali>();
}
if (this.turnikeIsKurali == null)
{
this.turnikeIsKurali = this.ninjectKernel.Get<ITurnikeIslemlerIsKurali>();
}
if (this.personelilisiklendirik == null)
{
this.personelilisiklendirik = this.ninjectKernel.Get<IPersonelIliskilendir>();
}
if (this.birimlerik == null)
{
this.birimlerik = this.ninjectKernel.Get<IBirimlerIsKurali>();
}
if (this.personelizinik == null)
{
this.personelizinik = this.ninjectKernel.Get<IPersonelIzinIsKurali>();
}
if (this.fs == null)
{
this.fs = new FonksiyonSonuc(true);
}
if (this.servisIsKurali == null)
{
this.servisIsKurali = this.ninjectKernel.Get<IServisIsKurali>();
}
}
}
What is wrong with it? Thanks in advance.
Edit-1: Here is the visual explanatory of the error:
I have just solve that problem. You can not set your ninject kernel in such manner. Implementing ninject kernel in asp.net web forms are expressed here:
How can I implement Ninject or DI on asp.net Web Forms?