i am developing a .net mvc app with entity framework and linq.
i need to prevent the application from handling update operation after evening 6.00 pm .
and release the update lock after morning 6.00 AM.
do we have any configuration in entity framework or in mvc design or in global.asax or in mvc filter to prevent database update operation in the application after 6.00 pm and releasing the update lock after morning 6.00 AM
You could override the method SaveChanges of your context and do the intervall check inside.
See the following link
public override int SaveChanges(SaveOptions options)
{
if(DateTime.Now.TimeOfDay.Hours < 6 || DateTime.Now.TimeOfDay.Hours >= 18) {
{
throw new Exception("Changes to database are not allowed");
}
else
{
return base.SaveChanges(options);
}
}
Related
I need to ensure my application itself is able to upgrade his database model (apply migrations)
In the ABP architecture, where should I make the call to Migrate?
context.Database.Migrate();
As this is a call to a infraestructure logic (Entity framework Core) it should be kept out from domain services and application services.
Thanks in advance
I finally found a working solution, for abp 4.3
This code allows your application to apply migrations at startup.
public override void PostInitialize()
{
var dbContextProvider = IocManager.Resolve<IDbContextProvider<ExtranetDbContext>>();
var unitOfWorkManager = IocManager.Resolve<IUnitOfWorkManager>();
using (var unitOfWork = unitOfWorkManager.Begin())
{
var context = dbContextProvider.GetDbContext(MultiTenancySides.Host);
//Removes actual connection as it has been enlisted in a non needed transaction for migration
context.Database.CloseConnection();
context.Database.Migrate();
}
if (!SkipDbSeed)
{
SeedHelper.SeedHostDb(IocManager);
}
}
Details about the IUnitOfWorkManager can be found here.
Hi you can execute database migrations in PostInitialize method in EntityFrameworkCoreModule.
public class MyApplicationEntityFrameworkCoreModule : AbpModule
{
public override void PostInitialize()
{
if (!SkipDbSeed)
{
SeedHelper.SeedHostDb(IocManager);
}
// --> You can execute migrations here <--
}
}
I Have a Multi-Tenant application and how can I use Interception in Entity Framework 7?
In Entity Framework 6, exists Interception using System.Data.Entity.Infrastructure.Interception, but don't find in version 7 of the Entity Framework.
Here's an example -> https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-entity-framework-row-level-security/
Interception isn't implemented yet in EFCore. It is a backlog item (see https://github.com/aspnet/EntityFramework/wiki/Roadmap)
Although EF Core does not have Interceptors, it is possible to perform QueryFilters to ensure that all queries are filtered by the tenant id.
Gunnar Peipman has a number of articles that can help you understand how to use QueryFilters for a Multi-Tenant scenario.
http://gunnarpeipman.com/2017/08/ef-core-global-query-filters/
I have somewhat some issue.
In EF Core you can use interceptors, here's some sample code that may be of use:
using System.Data.Common;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DiagnosticAdapter;
public class CommandListener
{
[DiagnosticName("Microsoft.EntityFrameworkCore.Database.Command.CommandExecuting")]
public void OnCommandExecuting(DbCommand command, DbCommandMethod executeMethod, Guid commandId, Guid connectionId, bool async, DateTimeOffset startTime)
{
//call security or other methods here.
}
[DiagnosticName("Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted")]
public void OnCommandExecuted(object result, bool async)
{
//call security or other methods here.
}
}
In the constructor of your repository you do the hooking
var listener = _context.GetService<DiagnosticSource>();
(listener as DiagnosticListener).SubscribeWithAdapter(new CommandListener());
Now when you query your dbContext e.g.:
_context.employees.Where(...
Then before this query is returned the above methods OnCommandExecuting and OnCommandExecuted are executed.
So you can somewhat imitate the override of SaveChanges in EF Core.
However one important thing to note is that the return result set from the query is not accessible in the OnCommandExecuting and OnCommandExecuted methods.
On my development machine I have the FormInitializing and FormShowing events firing before RibbonLoad. I created a setup package in VS 2010 and installed on a vanilla Windows 7 Ultimate with Outlook 2010 installed.
The addin wasn't appearing on my meeting request form. So I setup remote debugger and to my astonishment the RibbonLoad is firing before the two form events mentioned above. A null exception is being throw b\c the code in the RibbonLoad relies on the FormRegion already being loaded. Can anyone offer any insight?
There is no defined order for certain Outlook events - the Ribbon UI and the Inspector UI are completely different components, despite them both being display in the same window. The Outlook runtime may trigger Ribbon and Inspector events in different orders. It would be your job to synchronize the two events (RibbonLoad and FormInitializing) if you need some initialization done. You cannot assume that the ordering will always be the same.
I notice this same behavior when ThisAddIn.Startup fires before ThisAddIn.CreateRibbonExtensibilityObject, but sometimes after depending on how Outlook triggers the sequencing. You can just use a static variable with sync locking to ensure your initialization code is only triggered once.
Here is an example I used to synchronize the Startup event with the RibbonLoad event:
public partial class ThisAddIn
{
static bool formInitialized = false;
static readonly object padLock = new Object();
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
lock(padLock) { if (!formInitialized ) { InitializeForm(); } }
// startup code
}
private void InitializeForm()
{
// init code
formInitialized = true;
}
protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
{
lock(padLock) { if (!formInitialized) InitializeForm(); }
// Create ribbon UI
}
}
when i used Nokia Exchange Add SDk i tried to work with add events when it fired and i found that in documentation :
public MainPage()
{
InitializeComponent();
InneractiveAd.AdClicked += new
InneractiveAd.IaAdClicked(InneractiveAd_AdClicked);
}
void InneractiveAd_AdClicked(object sender)
{
System.Diagnostics.Debug.WriteLine("InneractiveAd: AdClicked");
}
when i click the add nothing happen and in debuging the no entrance for this method which mean that event didn't fire.
so is there any mistakes in this code ? or there is another way to do that?
There's a new version inneractiveAdSDK-Nokia-WP8-v1.1.2 for Windows Phone 8 recently released. Update your SDK and it should fix the issue.
Try this:
public MainPage()
{
InitializeComponent();
InneractiveAd.AdClicked += InneractiveAd_AdClicked;
}
void InneractiveAd_AdClicked(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("InneractiveAd: AdClicked");
}
This was solved in v1.1.2 as mentioned in release notes :
inneractiveAdSDK-Nokia-WP8-v1.1.2
- Release date: 1/29/2013
- Supports WP 8.0
- Add optional parameters for ad sizes supports. Banner's Required/Optional parameters are only relevant for C#.
- Additional Ad alignment optional parameters.
- Events are no longer static.
- Events bug was resolved.
- Validation handling was added.
- Improve the refresh ad handling.
- Performance was improved.
I'm getting an error when trying to run the EF 4.3.1 add-migrations command:
"The model backing the ... context has changed since the database was created".
Here's one sequence that gets the error (although I've tried probably a dozen variants which also all fail)...
1) Start with a database that was created by EF Code First (ie, already contains a _MigrationHistory table with only the InitialCreate row).
2) The app's code data model and database are in-sync at this point (the database was created by CF when the app was started).
3) Because I have four DBContexts in my "Services" project, I didn't run 'enable-migrations' command (it doesn't handle multipe contexts). Instead, I manually created the Migrations folder in the Services project and the Configuration.cs file (included at end of this post). [I think I read this in a post somewhere]
4) With the database not yet changed, and the app stopped, I use the VS EDM editor to make a trivial change to my data model (add one property to an existing entity), and have it generate the new classes (but not modify the database, obviously). I then rebuild the solution and all looks OK (but don't delete the database or restart the app, of course).
5) I run the following PMC command (where "App" is the name of one of the classes in Configuration.cs):
PM> add-migration App_AddTrivial -conf App -project Services -startup Services -verbose
... which fails with the "The model ... has changed. Consider using Code First Migrations..." error.
What am I doing wrong? And does anyone else see the irony in the tool telling me to use what I'm already trying to use ;-)
What are the correct steps for setting-up a solution starting with a database that was created by EF CF? I've seen posts saying to run an initial migration with -ignorechanges, but I've tried that and it doesn't help. Actually, I've spent all DAY testing various permutations, and nothing works!
I must be doing something really stupid, but I don't know what!
Thanks,
DadCat
Configuration.cs:
namespace mynamespace
{
internal sealed class App : DbMigrationsConfiguration
{
public App()
{
AutomaticMigrationsEnabled = false;
MigrationsNamespace = "Services.App.Repository.Migrations";
}
protected override void Seed(.Services.App.Repository.ModelContainer context)
{
}
}
internal sealed class Catalog : DbMigrationsConfiguration<Services.Catalog.Repository.ModelContainer>
{
public Catalog()
{
AutomaticMigrationsEnabled = false;
MigrationsNamespace = "Services.Catalog.Repository.Migrations";
}
protected override void Seed(Services.Catalog.Repository.ModelContainer context)
{
}
}
internal sealed class Portfolio : DbMigrationsConfiguration<Services.PortfolioManagement.Repository.ModelContainer>
{
public Portfolio()
{
AutomaticMigrationsEnabled = false;
MigrationsNamespace = "Services.PortfolioManagement.Repository.Migrations";
}
protected override void Seed(Services.PortfolioManagement.Repository.ModelContainer context)
{
}
}
internal sealed class Scheduler : DbMigrationsConfiguration<.Services.Scheduler.Repository.ModelContainer>
{
public Scheduler()
{
AutomaticMigrationsEnabled = false;
MigrationsNamespace = "Services.Scheduler.Repository.Migrations";
}
protected override void Seed(Services.Scheduler.Repository.ModelContainer context)
{
}
}
}
When using EF Migrations you should have one data context per database. I know that it can grow really large, but by trying to split it you will run into several problems. One is the migration issue that you are experiencing. Later on you will probably be facing problems when trying to make queries joining tables from the different contexts. Don't go that way, it's against how EF is designed.