title: Unable to determine the principal end of an association - Entity Framework Model First
body: <p>I have created Entity Data Model in Visual Studio. Now I have file with SQL queries and C# classes generated from Model.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Classes are generated without annotations or code behind (Fluent API). Is it OK? I tried to run my application but exception was thrown:</p>
  
  <p>Unable to determine the principal end of an association between the types 'Runnection.Models.Address' and 'Runnection.Models.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.</p>
  
  <p>I read that I can not use Fluent API with "Model First". So what can I do? </p>
  
  <p><strong>Code:</strong></p>
  
  <p><em>User</em></p>
  
  <pre><code>public partial class User
  {
      public User()
      {
          this.Events = new HashSet&lt;Event&gt;();
          this.CreatedEvents = new HashSet&lt;Event&gt;();
      }
  
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Photo { get; set; }
      public int EventId { get; set; }
      public string Nickname { get; set; }
      public OwnerType OwnerType { get; set; }
      public NetworkPlaceType PlaceType { get; set; }
  
      public virtual ICollection&lt;Event&gt; Events { get; set; }
      public virtual Address Address { get; set; }
      public virtual ICollection&lt;Event&gt; CreatedEvents { get; set; }
      public virtual Owner Owner { get; set; }
  }
  </code></pre>
  
  <p><em>Address</em></p>
  
  <pre><code>public partial class Address
  {
      public int Id { get; set; }
      public string Street { get; set; }
      public string StreetNumber { get; set; }
      public string City { get; set; }
      public string ZipCode { get; set; }
      public string Country { get; set; }
  
      public virtual User User { get; set; }
  }
  </code></pre>
  
  <p><em>Context</em></p>
  
  <p><em>//Model First does not use this method</em></p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Address&gt;().HasRequired(address =&gt; address.User)
                                     .WithRequiredDependent();
          modelBuilder.Entity&lt;User&gt;().HasRequired(user =&gt; user.Address)
                                     .WithRequiredPrincipal();
  
          base.OnModelCreating(modelBuilder);
      }
  </code></pre>