title: EF Core SQLITE - SQLite Error 19: 'UNIQUE constraint failed
body: <p>I am learning entity framework core, using it with SQLITE
  I have 2 tables in the database.</p>
  <p>Messages:</p>
  <pre><code>CREATE TABLE `Messages` (
      `Id`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
      `IsDeleted` INTEGER NOT NULL,
      `FromUserId`    INTEGER,
      `ToUserId`  INTEGER,
      `SendDate`  TEXT NOT NULL,
      `ReadDate`  TEXT NOT NULL,
      `MessageContent`    TEXT,
      CONSTRAINT `FK_Messages_Users_ToUserId` FOREIGN KEY(`ToUserId`) REFERENCES `Users`(`Id`) ON DELETE RESTRICT,
      CONSTRAINT `FK_Messages_Users_FromUserId` FOREIGN KEY(`FromUserId`) REFERENCES `Users`(`Id`) ON DELETE RESTRICT
  );
  </code></pre>
  <p>and Users table:</p>
  <pre><code>CREATE TABLE `Users` (
      `Id`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
      `IsDeleted` INTEGER NOT NULL,
      `Name`  TEXT,
      `DisplayName`   TEXT,
      `Gender`    INTEGER NOT NULL,
      `BirthDate` TEXT NOT NULL
  );
  </code></pre>
  <p>my c# classes look like:</p>
  <pre><code>public class User
  {
      public int Id {get;set;}
      public bool IsDeleted{get;set;}
      [Required]
      public string Name{get;set;}
  
      public string DisplayName{get;set;}
  
      [Required]
      public char Gender{get;set;}
  
      [Required]
      public DateTime BirthDate{get;set;}
  
      public User(string n, string dn, char g, DateTime bd)
      {
          Name=n; DisplayName = dn; Gender = g; BirthDate = bd;
      }
  
      protected User(){}
  
      public override string ToString()
      {
          return string.Format(&quot;{0} ({1}) {2}&quot;, this.Name, this.Gender,this.BirthDate.ToShortDateString());
      }
  }
  </code></pre>
  <p>and Messages class:</p>
  <pre><code>public class Message 
  {
      public int Id{get;set;}
      public bool IsDeleted{get;set;}
      [Required]
      public Users.User FromUser{get;set;}
      
      [Required]
      public Users.User ToUser{get;set;}
  
      [Required]
      public DateTime SendDate{get;set;}
      public DateTime? ReadDate{get;set;}
  
      [Required]
      public string MessageContent{get;set;}
  
      protected Message(){}
  
      public Message(User from, User to, string content)
      {
          this.FromUser = from;
          this.ToUser = to;
          this.MessageContent = content;
          this.SendDate = DateTime.Now;
          this.ReadDate = DateTime.Now;
      }
  
  }
  </code></pre>
  <p>users table works, but when I try to add a new entity to the messages by this:</p>
  <pre><code>public override int Insert(Message entity)
  {
      dbc.Messages.Add(entity);
      dbc.SaveChanges();
      return entity.Id;
  }
  </code></pre>
  <p>I get the following error:</p>
  <blockquote>
  <p>SQLite Error 19: 'UNIQUE constraint failed</p>
  </blockquote>
  <p>I have no idea what is wrong. When I manually inserted data to a database (using DB browser for sqlite) it works.
  Are the relationships in the classes ok?</p>
  <p>My <code>dbContextSnapshot</code> is:</p>
  <pre><code>[DbContext(typeof(RandevouDbContext))]
  partial class RandevouDbContextModelSnapshot : ModelSnapshot
  {
      protected override void BuildModel(ModelBuilder modelBuilder)
      {
  #pragma warning disable 612, 618
      modelBuilder.HasAnnotation(&quot;ProductVersion&quot;, &quot;2.1.4-rtm-31024&quot;);
  
      modelBuilder.Entity(&quot;RandevouData.Messages.Message&quot;, b =&gt;
          {
              b.Property&lt;int&gt;(&quot;Id&quot;)
                  .ValueGeneratedOnAdd();
  
              b.Property&lt;int&gt;(&quot;FromUserId&quot;);
  
              b.Property&lt;bool&gt;(&quot;IsDeleted&quot;);
  
              b.Property&lt;string&gt;(&quot;MessageContent&quot;)
                  .IsRequired();
  
              b.Property&lt;DateTime?&gt;(&quot;ReadDate&quot;);
  
              b.Property&lt;DateTime&gt;(&quot;SendDate&quot;);
  
              b.Property&lt;int&gt;(&quot;ToUserId&quot;);
  
              b.HasKey(&quot;Id&quot;);
  
              b.HasIndex(&quot;FromUserId&quot;);
  
              b.HasIndex(&quot;ToUserId&quot;);
  
              b.ToTable(&quot;Messages&quot;);
          });
  
      modelBuilder.Entity(&quot;RandevouData.Users.User&quot;, b =&gt;
          {
              b.Property&lt;int&gt;(&quot;Id&quot;)
                  .ValueGeneratedOnAdd();
  
              b.Property&lt;DateTime&gt;(&quot;BirthDate&quot;);
  
              b.Property&lt;string&gt;(&quot;DisplayName&quot;);
  
              b.Property&lt;char&gt;(&quot;Gender&quot;);
  
              b.Property&lt;bool&gt;(&quot;IsDeleted&quot;);
  
              b.Property&lt;string&gt;(&quot;Name&quot;)
                  .IsRequired();
  
              b.HasKey(&quot;Id&quot;);
  
              b.ToTable(&quot;Users&quot;);
          });
  
      modelBuilder.Entity(&quot;RandevouData.Messages.Message&quot;, b =&gt;
          {
              b.HasOne(&quot;RandevouData.Users.User&quot;, &quot;FromUser&quot;)
                  .WithMany()
                  .HasForeignKey(&quot;FromUserId&quot;)
                  .OnDelete(DeleteBehavior.Cascade);
  
              b.HasOne(&quot;RandevouData.Users.User&quot;, &quot;ToUser&quot;)
                  .WithMany()
                  .HasForeignKey(&quot;ToUserId&quot;)
                  .OnDelete(DeleteBehavior.Cascade);
          });
  #pragma warning restore 612, 618
  }
  </code></pre>
  <p>By the way. I cannot configure mapping from two sides.</p>
  <p>The Message entity has UserFrom and UserTo fields,
  but the User entity can not have Messages, because one time he is &quot;userFrom&quot; and other time he can be &quot;userTo&quot;.</p>
  <p>And there is also a code, where i create Message entity</p>
  <pre><code>        public int SendMessage(int senderId, int receiverId, string content)
      {
          var dao = new MessagesDao();
          var usersDao = new UsersDao();
          var userService = new UserService(mapper);
          
          var sender = usersDao.Get(senderId);
          var receiver = usersDao.Get(receiverId);
          
          var entity = new Message(sender,receiver,content);
          var id = dao.Insert(entity);
          return id;
      }
  </code></pre>
  <p>user dao where i override GET method</p>
  <pre><code> public override User Get(int id)
          {
              using (var context = new RandevouDbContext())
              { 
                  var user = dbc.Find&lt;User&gt;(id);
                  return user;
              }
          }
  </code></pre>
  <p>and MessagesDao where i override Add method</p>
  <pre><code> public override int Insert(Message entity)
          {
              using (var dc = new RandevouDbContext())
              {
                  dc.Messages.Add(entity);
                  dc.SaveChanges();
                  return entity.Id;
              }
          }
  </code></pre>
  <p>btw, i dont know if it is ok, but i have 0 entries to update (!?)
  <a href="https://i.stack.imgur.com/Sm0wx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Sm0wx.png" alt="enter image description here" /></a></p>