Skip to content

Instantly share code, notes, and snippets.

public virtual void Update(TEntity entityToUpdate)
{
var entry = this.context.Entry(entityToUpdate);
var key = this.GetPrimaryKey(entry);
if (entry.State == EntityState.Detached)
{
var currentEntry = this.databaseSet.Find(key);
if (currentEntry != null)
{
private int GetPrimaryKey(DbEntityEntry entry)
{
var myObject = entry.Entity;
var property =
myObject.GetType()
.GetProperties()
.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute)));
return (int)property.GetValue(myObject, null);
}
using (var context = new YourDbContext())
{
context.Database.Log = Console.Write;
// rest of you code goes here...
}
using (var context = new YourDbContext())
{
context.Database.Log = message => Trace.Write(message);
// rest of you code goes here
}
using Common.Logging;
public class SomeClass
{
private static readonly ILog log = LogManager.GetCurrentClassLogger();
public void SomeMethod()
{
context.Database.Log = message => log.Debug(message);
}
using Common.Logging;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Data.Entity.SqlServer;
using System.Data.SqlClient;
using System.Reflection;
using System.Linq;
public class CustomEFInterceptor : IDbCommandInterceptor
//1. Application start up
protected void Application_Start()
{
// other code ommitted
DbInterception.Add(new YourInterceptor());
}
//2. DbConfiguration
public class YourDBContextConfiguration : DbConfiguration
{
public sealed class BackgroundValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var passwordStrength = int.Parse(value.ToString());
if (passwordStrength > 33 && passwordStrength <= 66)
{
return new SolidColorBrush(Colors.Orange);
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pd="using:TestApp.Converters"
mc:Ignorable="d">
<!-- Other styles omitted for clarity -->
<pd:BackgroundValueConverter x:Key="BackgroundValueConverter"></pd:BackgroundValueConverter>
</ResourceDictionary>
<ProgressBar Name="PrgStrength"
IsIndeterminate="False"
HorizontalAlignment="Stretch"
Margin="2,11,0,11"
Value="{Binding StrengthValue, Mode=TwoWay}"
Foreground="{Binding Path=StrengthValue, Converter={StaticResource BackgroundValueConverter}}"
Width="320">
</ProgressBar>