Note that there are some explanatory texts on larger screens.

plurals
  1. POEF5, code first exception ONLY on first database access: "FK_x...x' is not a constraint. Could not drop constraint"
    primarykey
    data
    text
    <p>This has been driving me crazy for days. I am using vs2012, EF5 code first, sql server 2012. This is on my development machine and <strong><em>I get the below exception when, and ONLY when, I hit the database the very first time</em></strong>. If, before I run my tests, I init the database, it throws the below exception, <strong>but not thereafter</strong>. If I run my tests, the first test fails because of this exception, <strong>but all my other tests pass</strong>. If I catch the exception and ignore it, <strong>all my tests pass without error</strong>. What is frustrating, is I've defined constraints, between those two tables, but don't know where 'Users_Id' is defined. I've also tried manually deleting the tables and running the vs2012 generated sql from my DbContext to recreate the table fresh, but nothing works. Any help would be much appreciated.</p> <p><strong>Here is the exception</strong></p> <p>*System.Data.SqlClient.SqlException: 'FK_dbo.UserProfiles_dbo.Users_Id' is not a constraint. Could not drop constraint. See previous errors.*</p> <p><strong>Here are my pocos</strong></p> <p><strong>User</strong></p> <pre><code>public class User : Entity&lt;User&gt;, IUser { public Guid Id { get; set; } // Nav property to UserProfile. public virtual UserProfile UserProfile { get; set; } public Guid SiteId { get; set; } public virtual Site Site { get; set; } } </code></pre> <p><strong>UserProfile</strong></p> <pre><code>public class UserProfile : Entity&lt;UserProfile&gt;, IUserProfile { // Points to User.Id public Guid Id { get; set; } // Nav property back to user. public virtual User User { get; set; } } </code></pre> <p><strong>Here's my code first definition of the two tables</strong> (other code left out for brevity):</p> <p><strong>EDIT: The relationship between the two tables is Id User.Id => UserProfile.Id</strong></p> <p><strong>User</strong></p> <pre><code>modelBuilder.Entity&lt;User&gt;().HasKey(p =&gt; p.Id); modelBuilder.Entity&lt;User&gt;().HasRequired(p =&gt; p.Site).WithMany(p =&gt; p.Users).HasForeignKey(p =&gt; p.SiteId); modelBuilder.Entity&lt;User&gt;() .HasRequired(p =&gt; p.UserProfile) .WithRequiredPrincipal(p =&gt; p.User) .WillCascadeOnDelete(true); </code></pre> <p><strong>UserProfile</strong></p> <pre><code>modelBuilder.Entity&lt;UserProfile&gt;().HasKey(p =&gt; p.Id); modelBuilder.Entity&lt;UserProfile&gt;().HasRequired(p =&gt; p.User); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload