title: PostSharp.Samples.Xaml + EntityFramework
body: <p>
  While converting PostSharp.Samples.Xaml sample to use EntityFramework, I am encountering "k__BackingField" problem mentioned in  <a href="https://stackoverflow.com/questions/10570925/postsharp-inserting-k-backing-field-into-entity-class-causing-database-generat/10571183#10571183">PostSharp inserting k__Backing Field into Entity Class, causing Database generation to fail</a>.  Can you show me how to use the proposed solution "Use MulticastAttributeUsageAttribute.AttributeTargets to restrict it to properties." without losing PostSharp's INotifyPropertyChanged?  I am trying to persuade the stakeholders by showcasing both EF and PS.
  </p>
  
  <p>Here's the change to AddressModel.cs</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Text;
  using PostSharp.Patterns.Contracts;
  using PostSharp.Patterns.Model;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace PostSharp.Samples.Xaml
  {
      public class AddressModel : ModelBase
    {
      public int AddressId { get; set; }
  
      [DisplayName("Address Line 1")]
      [Patterns.Contracts.Required]
      public string Line1 { get; set; }
  
      [DisplayName("Address Line 2")]
      public string Line2 { get; set; }
  
      [Patterns.Contracts.Required]
      public string Town { get; set; }
  
      public string Country { get; set; }
  
      public DateTime Expiration { get; set; }
  
      [IgnoreAutoChangeNotification]
      public TimeSpan Lifetime =&gt; DateTime.Now - Expiration;
  
      [SafeForDependencyAnalysis]
      public string FullAddress
      {
          get
          {
              var stringBuilder = new StringBuilder();
              if (Line1 != null) stringBuilder.Append(Line1);
              if (Line2 != null)
              {
                  if (stringBuilder.Length &gt; 0) stringBuilder.Append("; ");
                  stringBuilder.Append(Line2);
              }
              if (Town != null)
              {
                  if (stringBuilder.Length &gt; 0) stringBuilder.Append("; ");
                  stringBuilder.Append(Town);
              }
              if (Country != null)
              {
                  if (stringBuilder.Length &gt; 0) stringBuilder.Append("; ");
                  stringBuilder.Append(Country);
              }
  
              return stringBuilder.ToString();
          }
      }
          [ForeignKey("AddressId")]
          [Parent]
          public virtual CustomerModel Person { get; set; }
      }
  }
  </code></pre>
  
  <p>app.config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
    &lt;/configSections&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="PostSharp" publicKeyToken="b13fd38b8f9c99d7" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-5.0.37.0" newVersion="5.0.37.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /&gt;
    &lt;/startup&gt;
    &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;parameter value="mssqllocaldb" /&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>CustomerModel.cs</p>
  
  <pre><code>using System.IO;
  using System.Threading;
  using PostSharp.Patterns.Collections;
  using PostSharp.Patterns.Contracts;
  using PostSharp.Patterns.Model;
  using PostSharp.Patterns.Threading;
  using System;
  using System.ComponentModel.DataAnnotations;
  using PostSharp.Extensibility;
  
  namespace PostSharp.Samples.Xaml
  {
      public class CustomerModel : ModelBase
    {
  
      [Key]
      public int CustomerId { get; set; }
  
      public string FirstName { get; set; }
  
      [Patterns.Contracts.Required]
      public string LastName { get; set; }
      public string Phone { get; set; }
      public string Mobile { get; set; }
      public string Email { get; set; }
  
      [Child]
      public AdvisableCollection&lt;AddressModel&gt; Addresses { get; set; }
  
      [Reference]
      public AddressModel PrincipalAddress { get; set; }
  
      [Reader]
      public void Save(string path)
      {
        using (var stringWriter = new StreamWriter(path))
        {
          // We need to make sure the object graph is not being modified when we save,
          // and this is ensured by [ReaderWriterSynchronized] in ModelBase.
  
          stringWriter.WriteLine($"UserID: {CustomerId}");
          //Thread.Sleep(1000);
  
          stringWriter.WriteLine($"FirstName: {FirstName}");
          //Thread.Sleep(1000);
  
          stringWriter.WriteLine($"LastName: {LastName}");
          //Thread.Sleep(1000);
  
          stringWriter.WriteLine($"Phone: {Phone}");
          //Thread.Sleep(1000);
  
          stringWriter.WriteLine($"Mobile: {Mobile}");
          //Thread.Sleep(1000);
  
          stringWriter.WriteLine($"Email: {Email}");
          //Thread.Sleep(1000);
  
          foreach (var address in Addresses)
          {
            //Thread.Sleep(1000);
  
            if (address == PrincipalAddress)
              stringWriter.WriteLine($"Principal address: {address}");
            else
              stringWriter.WriteLine($"Secondary address: {address}");
          }
  
          try
          {
              using (var ctx = new EFContext())
              {
                  ctx.Person.Add(this);
                  ctx.SaveChanges();
              }
  
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
  
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="PostSharp.Samples.Xaml.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:xaml="clr-namespace:PostSharp.Samples.Xaml"
          xmlns:controls="clr-namespace:PostSharp.Patterns.Model.Controls;assembly=PostSharp.Patterns.Xaml"
          Title="Contact Form" Height="352.584" Width="352.753"
          x:Name="Window"&gt;
      &lt;Window.DataContext&gt;
          &lt;xaml:CustomerViewModel /&gt;
      &lt;/Window.DataContext&gt;
      &lt;DockPanel&gt;
          &lt;ToolBarTray DockPanel.Dock="Top"&gt;
              &lt;ToolBar&gt;
                  &lt;Button Name="SaveButton" Command="{Binding ElementName=Window, Path=SaveCommand}"&gt;Save&lt;/Button&gt;
                  &lt;controls:UndoButton /&gt;
                  &lt;controls:RedoButton /&gt;
              &lt;/ToolBar&gt;
          &lt;/ToolBarTray&gt;
          &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="29*" /&gt;
                  &lt;ColumnDefinition Width="316*" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Label Content="First Name:" HorizontalAlignment="Left" Margin="10,62,0,0" VerticalAlignment="Top"
                     Grid.ColumnSpan="2" /&gt;
              &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="97.846,62,0,0" TextWrapping="Wrap"
                       Text="{Binding Path=Customer.FirstName, ValidatesOnExceptions=True}" VerticalAlignment="Top"
                       Width="189" Grid.Column="1" /&gt;
  
              &lt;Label Content="Last Name:" HorizontalAlignment="Left" Margin="10,88,0,0" VerticalAlignment="Top"
                     Grid.ColumnSpan="2" /&gt;
              &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="97.846,92,0,0" TextWrapping="Wrap"
                       Text="{Binding Path=Customer.LastName, ValidatesOnExceptions=True}" VerticalAlignment="Top"
                       Width="189" Grid.Column="1" /&gt;
              &lt;Label Content="Principal Address:" HorizontalAlignment="Left" Margin="10,119,0,0" VerticalAlignment="Top"
                     RenderTransformOrigin="-0.105,-0.462" Grid.ColumnSpan="2" /&gt;
              &lt;ComboBox HorizontalAlignment="Left" Margin="97.846,120,0,0" VerticalAlignment="Top" Width="189"
                        ItemsSource="{Binding Path=Customer.Addresses}"
                        SelectedValue="{Binding Path=Customer.PrincipalAddress}" DisplayMemberPath="FullAddress"
                        Grid.Column="1" /&gt;
              &lt;xaml:FancyTextBlock HorizontalAlignment="Left" Margin="10,15,0,0" Text="{Binding Path=FullName}"
                                   VerticalAlignment="Top" Width="214" Grid.ColumnSpan="2" /&gt;
              &lt;Label Content="Line1:" HorizontalAlignment="Left" Margin="11.846,150,0,0" VerticalAlignment="Top"
                     Grid.Column="1" /&gt;
              &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="97.846,154,0,0" TextWrapping="Wrap"
                       Text="{Binding Path=Customer.PrincipalAddress.Line1, ValidatesOnExceptions=True}"
                       VerticalAlignment="Top" Width="189" Grid.Column="1" /&gt;
              &lt;Label Content="Town:" HorizontalAlignment="Left" Margin="12.846,176,0,0" VerticalAlignment="Top"
                     Grid.Column="1" /&gt;
              &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="97.846,182,0,0" TextWrapping="Wrap"
                       Text="{Binding Path=Customer.PrincipalAddress.Town, ValidatesOnExceptions=True}"
                       VerticalAlignment="Top" Width="189" Grid.Column="1" /&gt;
              &lt;Label Content="User Id:" HorizontalAlignment="Left" Margin="11.846,210,0,0" VerticalAlignment="Top"
                     Grid.ColumnSpan="2" /&gt;
              &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="97.846,210,0,0" TextWrapping="Wrap"
                       Text="{Binding Path=Customer.UserID, ValidatesOnExceptions=True}" VerticalAlignment="Top"
                       Width="189" Grid.Column="1" /&gt;
  
          &lt;/Grid&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Input;
  using Microsoft.Win32;
  using PostSharp.Patterns.Collections;
  using PostSharp.Patterns.Model;
  using PostSharp.Patterns.Recording;
  using PostSharp.Patterns.Threading;
  using PostSharp.Patterns.Xaml;
  
  namespace PostSharp.Samples.Xaml
  {
      /// &lt;summary&gt;
      ///   Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      [NotifyPropertyChanged]
      public partial class MainWindow : Window
      {
          /* private readonly */
          public readonly CustomerModel customer = new CustomerModel
          {
              Email = "2",
              Mobile = "3",
              Phone = "4",
              CustomerId = 1,
              FirstName = "Jan",
              LastName = "Novak",
              Addresses = new AdvisableCollection&lt;AddressModel&gt;
        {
          new AddressModel
          {
            Line1 = "Saldova 1G",
            Town = "Prague",
            Country = "USA"
          },
          new AddressModel
          {
            Line1 = "Tyrsova 25",
            Town = "Brno",
            Country = "USA"
  
          },
          new AddressModel
          {
            Line1 = "Pivorarka 154",
            Town = "Pilsen",
            Country = "USA"
  
          }
        }
          };
  
          private readonly Recorder recorder;
  
          public MainWindow()
          {
              // We need to have a local reference for [NotifyPropertyChanged] to work.
              recorder = RecordingServices.DefaultRecorder;
  
  
              InitializeComponent();
  
              // Register our custom operation formatter.
              RecordingServices.OperationFormatter = new MyOperationFormatter(RecordingServices.OperationFormatter);
  
        // Create initial data.
        var customerViewModel = new CustomerViewModel {Customer = customer};
  
        customerViewModel.Customer.PrincipalAddress = customerViewModel.Customer.Addresses[0];
  
              // Clear the initialization steps from the recorder.
              recorder.Clear();
  
        DataContext = customerViewModel;
      }
  
          [Command]
          public ICommand SaveCommand { get; private set; }
  
          public bool CanExecuteSave =&gt; recorder.UndoOperations.Count &gt; 0;
  
          private void ExecuteSave()
          {
              var openFileDialog = new SaveFileDialog();
  
              if (openFileDialog.ShowDialog().GetValueOrDefault())
                  Save(openFileDialog.FileName);
          }
  
  
          [Background]
          [DisableUI]
          private void Save(string path)
          {
              customer.Save(path);
          }
      }
  }
  </code></pre>
  
  <p>packages.config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;packages&gt;
    &lt;package id="EntityFramework" version="6.2.0" targetFramework="net461" /&gt;
    &lt;package id="PostSharp" version="5.0.40" targetFramework="net461" developmentDependency="true" /&gt;
    &lt;package id="PostSharp.Patterns.Aggregation" version="5.0.40" targetFramework="net461" developmentDependency="true" /&gt;
    &lt;package id="PostSharp.Patterns.Aggregation.Redist" version="5.0.40" targetFramework="net461" /&gt;
    &lt;package id="PostSharp.Patterns.Common" version="5.0.40" targetFramework="net461" developmentDependency="true" /&gt;
    &lt;package id="PostSharp.Patterns.Common.Redist" version="5.0.40" targetFramework="net461" /&gt;
    &lt;package id="PostSharp.Patterns.Model" version="5.0.40" targetFramework="net461" developmentDependency="true" /&gt;
    &lt;package id="PostSharp.Patterns.Model.Redist" version="5.0.40" targetFramework="net461" /&gt;
    &lt;package id="PostSharp.Patterns.Threading" version="5.0.40" targetFramework="net461" developmentDependency="true" /&gt;
    &lt;package id="PostSharp.Patterns.Threading.Redist" version="5.0.40" targetFramework="net461" /&gt;
    &lt;package id="PostSharp.Patterns.Xaml" version="5.0.40" targetFramework="net461" developmentDependency="true" /&gt;
    &lt;package id="PostSharp.Patterns.Xaml.Redist" version="5.0.40" targetFramework="net461" /&gt;
    &lt;package id="PostSharp.Redist" version="5.0.40" targetFramework="net461" /&gt;
  &lt;/packages&gt;
  </code></pre>
  
  <p>PostSharp.Samples.Xaml.csproj</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;Import Project="..\..\packages\PostSharp.5.0.40\build\PostSharp.props" Condition="Exists('..\..\packages\PostSharp.5.0.40\build\PostSharp.props')" /&gt;
    &lt;Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /&gt;
    &lt;PropertyGroup&gt;
      &lt;Configuration Condition=" '$(Configuration)' == '' "&gt;Debug&lt;/Configuration&gt;
      &lt;Platform Condition=" '$(Platform)' == '' "&gt;AnyCPU&lt;/Platform&gt;
      &lt;ProjectGuid&gt;{B10B8456-75F7-4D68-9775-CC87517B07B6}&lt;/ProjectGuid&gt;
      &lt;OutputType&gt;WinExe&lt;/OutputType&gt;
      &lt;AppDesignerFolder&gt;Properties&lt;/AppDesignerFolder&gt;
      &lt;RootNamespace&gt;PostSharp.Samples.Xaml&lt;/RootNamespace&gt;
      &lt;AssemblyName&gt;PostSharp.Samples.Xaml&lt;/AssemblyName&gt;
      &lt;TargetFrameworkVersion&gt;v4.6.1&lt;/TargetFrameworkVersion&gt;
      &lt;FileAlignment&gt;512&lt;/FileAlignment&gt;
      &lt;ProjectTypeGuids&gt;{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&lt;/ProjectTypeGuids&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
      &lt;NuGetPackageImportStamp&gt;
      &lt;/NuGetPackageImportStamp&gt;
      &lt;LangVersion&gt;6&lt;/LangVersion&gt;
      &lt;TargetFrameworkProfile /&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "&gt;
      &lt;PlatformTarget&gt;AnyCPU&lt;/PlatformTarget&gt;
      &lt;DebugSymbols&gt;true&lt;/DebugSymbols&gt;
      &lt;DebugType&gt;full&lt;/DebugType&gt;
      &lt;Optimize&gt;false&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\Debug\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;DEBUG;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
      &lt;Prefer32Bit&gt;false&lt;/Prefer32Bit&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "&gt;
      &lt;PlatformTarget&gt;AnyCPU&lt;/PlatformTarget&gt;
      &lt;DebugType&gt;pdbonly&lt;/DebugType&gt;
      &lt;Optimize&gt;true&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\Release\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
      &lt;Prefer32Bit&gt;false&lt;/Prefer32Bit&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"&gt;
        &lt;HintPath&gt;..\..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"&gt;
        &lt;HintPath&gt;..\..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="PostSharp, Version=5.0.40.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7"&gt;
        &lt;HintPath&gt;..\..\packages\PostSharp.Redist.5.0.40\lib\net35-client\PostSharp.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="PostSharp.Patterns.Aggregation"&gt;
        &lt;HintPath&gt;..\..\packages\PostSharp.Patterns.Aggregation.Redist.5.0.40\lib\net45\PostSharp.Patterns.Aggregation.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="PostSharp.Patterns.Common"&gt;
        &lt;HintPath&gt;..\..\packages\PostSharp.Patterns.Common.Redist.5.0.40\lib\net46\PostSharp.Patterns.Common.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="PostSharp.Patterns.Model"&gt;
        &lt;HintPath&gt;..\..\packages\PostSharp.Patterns.Model.Redist.5.0.40\lib\net40\PostSharp.Patterns.Model.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="PostSharp.Patterns.Threading"&gt;
        &lt;HintPath&gt;..\..\packages\PostSharp.Patterns.Threading.Redist.5.0.40\lib\net45\PostSharp.Patterns.Threading.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="PostSharp.Patterns.Xaml"&gt;
        &lt;HintPath&gt;..\..\packages\PostSharp.Patterns.Xaml.Redist.5.0.40\lib\net40\PostSharp.Patterns.Xaml.dll&lt;/HintPath&gt;
        &lt;Private&gt;True&lt;/Private&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="System" /&gt;
      &lt;Reference Include="System.ComponentModel.DataAnnotations" /&gt;
      &lt;Reference Include="System.Data" /&gt;
      &lt;Reference Include="System.Xml" /&gt;
      &lt;Reference Include="Microsoft.CSharp" /&gt;
      &lt;Reference Include="System.Core" /&gt;
      &lt;Reference Include="System.Xml.Linq" /&gt;
      &lt;Reference Include="System.Data.DataSetExtensions" /&gt;
      &lt;Reference Include="System.Xaml"&gt;
        &lt;RequiredTargetFramework&gt;4.0&lt;/RequiredTargetFramework&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="WindowsBase" /&gt;
      &lt;Reference Include="PresentationCore" /&gt;
      &lt;Reference Include="PresentationFramework" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ApplicationDefinition Include="App.xaml"&gt;
        &lt;Generator&gt;MSBuild:Compile&lt;/Generator&gt;
        &lt;SubType&gt;Designer&lt;/SubType&gt;
      &lt;/ApplicationDefinition&gt;
      &lt;Page Include="FancyTextBlock.xaml"&gt;
        &lt;SubType&gt;Designer&lt;/SubType&gt;
        &lt;Generator&gt;MSBuild:Compile&lt;/Generator&gt;
      &lt;/Page&gt;
      &lt;Page Include="MainWindow.xaml"&gt;
        &lt;Generator&gt;MSBuild:Compile&lt;/Generator&gt;
        &lt;SubType&gt;Designer&lt;/SubType&gt;
      &lt;/Page&gt;
      &lt;Compile Include="AddressModel.cs" /&gt;
      &lt;Compile Include="App.xaml.cs"&gt;
        &lt;DependentUpon&gt;App.xaml&lt;/DependentUpon&gt;
        &lt;SubType&gt;Code&lt;/SubType&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="CustomerModel.cs" /&gt;
      &lt;Compile Include="CustomerViewModel.cs" /&gt;
      &lt;Compile Include="DisableUIAttribute.cs" /&gt;
      &lt;Compile Include="EFContext.cs" /&gt;
      &lt;Compile Include="FancyTextBlock.xaml.cs"&gt;
        &lt;DependentUpon&gt;FancyTextBlock.xaml&lt;/DependentUpon&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="MainWindow.xaml.cs"&gt;
        &lt;DependentUpon&gt;MainWindow.xaml&lt;/DependentUpon&gt;
        &lt;SubType&gt;Code&lt;/SubType&gt;
      &lt;/Compile&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Compile Include="ModelBase.cs" /&gt;
      &lt;Compile Include="MyOperationFormatter.cs" /&gt;
      &lt;Compile Include="Properties\AssemblyInfo.cs"&gt;
        &lt;SubType&gt;Code&lt;/SubType&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="Properties\Resources.Designer.cs"&gt;
        &lt;AutoGen&gt;True&lt;/AutoGen&gt;
        &lt;DesignTime&gt;True&lt;/DesignTime&gt;
        &lt;DependentUpon&gt;Resources.resx&lt;/DependentUpon&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="Properties\Settings.Designer.cs"&gt;
        &lt;AutoGen&gt;True&lt;/AutoGen&gt;
        &lt;DependentUpon&gt;Settings.settings&lt;/DependentUpon&gt;
        &lt;DesignTimeSharedInput&gt;True&lt;/DesignTimeSharedInput&gt;
      &lt;/Compile&gt;
      &lt;EmbeddedResource Include="Properties\Resources.resx"&gt;
        &lt;Generator&gt;ResXFileCodeGenerator&lt;/Generator&gt;
        &lt;LastGenOutput&gt;Resources.Designer.cs&lt;/LastGenOutput&gt;
      &lt;/EmbeddedResource&gt;
      &lt;None Include="app.config" /&gt;
      &lt;None Include="packages.config" /&gt;
      &lt;None Include="Properties\Settings.settings"&gt;
        &lt;Generator&gt;SettingsSingleFileGenerator&lt;/Generator&gt;
        &lt;LastGenOutput&gt;Settings.Designer.cs&lt;/LastGenOutput&gt;
      &lt;/None&gt;
      &lt;AppDesigner Include="Properties\" /&gt;
      &lt;None Include="README.md" /&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /&gt;
    &lt;Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"&gt;
      &lt;PropertyGroup&gt;
        &lt;ErrorText&gt;This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.&lt;/ErrorText&gt;
      &lt;/PropertyGroup&gt;
      &lt;Error Condition="!Exists('..\..\packages\PostSharp.5.0.40\build\PostSharp.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.5.0.40\build\PostSharp.props'))" /&gt;
      &lt;Error Condition="!Exists('..\..\packages\PostSharp.5.0.40\build\PostSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PostSharp.5.0.40\build\PostSharp.targets'))" /&gt;
    &lt;/Target&gt;
    &lt;Import Project="..\..\packages\PostSharp.5.0.40\build\PostSharp.targets" Condition="Exists('..\..\packages\PostSharp.5.0.40\build\PostSharp.targets')" /&gt;
    &lt;!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
         Other similar extension points exist, see Microsoft.Common.targets.
    &lt;Target Name="BeforeBuild"&gt;
    &lt;/Target&gt;
    &lt;Target Name="AfterBuild"&gt;
    &lt;/Target&gt;
    --&gt;
  </code></pre>
  
  <p>Everything else remain the same, and you should be able to build it and see the exception in CustomerModel.cs line#80 when you change any field in the app and try to save.</p>
  
  
  
  
  
  <p>You get following ex.Message;</p>
  
  <pre><code>One or more validation errors were detected during model generation:
  
  PostSharp.Samples.Xaml.AddressModel: : EntityType 'AddressModel' has no key defined. Define the key for this EntityType.
  &lt;Person&gt;k__BackingField: Name: The specified name is not allowed: '&lt;Person&gt;k__BackingField'.
  PostSharp.Samples.Xaml.AddressModel_&lt;Person&gt;k__BackingField: Name: The specified name is not allowed: 'AddressModel_&lt;Person&gt;k__BackingField'.
  AddressModel_&lt;Person&gt;k__BackingField_Source: Name: The specified name is not allowed: 'AddressModel_&lt;Person&gt;k__BackingField_Source'.
  AddressModel_&lt;Person&gt;k__BackingField_Target: Name: The specified name is not allowed: 'AddressModel_&lt;Person&gt;k__BackingField_Target'.
  AddressModels: EntityType: EntitySet 'AddressModels' is based on type 'AddressModel' that has no keys defined.
  AddressModel_&lt;Person&gt;k__BackingField: Name: The specified name is not allowed: 'AddressModel_&lt;Person&gt;k__BackingField'.
  </code></pre>
  
  <p>A new file that was not in the original sample solution, EFContext.cs:</p>
  
  <pre><code>using System.Data.Entity;
  
  namespace PostSharp.Samples.Xaml
  {
      public class EFContext :DbContext
      {
          public DbSet&lt;CustomerModel&gt; Person { get; set; }
      }
  }
  </code></pre>