Foreign Key Constraints not created SqliteNetExtensions nuget package

Salman Paul 21 Reputation points
2020-06-29T15:15:37+00:00

Please help me in this matter. I am trying to create a Foreign Key. But not created. Please check the following Code and screenshot.

1- Create Class Users

public class Users  
    {  
        [PrimaryKey]  
        public int UserID { get; set; }  
        public string UserName { get; set; }  
  
        public string UserPassword { get; set; }  
  
        [OneToMany]  
        public List<AttendanceRecord> AttendanceRecords { get; set; }  
  
    }  

2- Create Class AttendanceRecord

public class AttendanceRecord  
    {  
        [PrimaryKey, AutoIncrement]  
        public int ID { get; set; }  
  
        [ForeignKey(typeof(Users))]  
        public int UserID { get; set; }  
        public DateTime InDate { get; set; }  
        public double InLatitude { get; set; }  
        public double InLongitude { get; set; }  
        public DateTime OutDate { get; set; }  
        public double OutLatitude { get; set; }  
        public double OutLongitude { get; set; }  
        public bool IsIsSynchronize { get; set;}  
  
         
  
    }  

3- Open Connection command

_database = new SQLiteAsyncConnection(dbPath);

4- Create Table Commands

_database.CreateTableAsync<Users>().Wait();
_database.CreateTableAsync<AttendanceRecord>().Wait();

5- Problem Foreign Key relation not created.
Please check the attached screenshot for reference

Please help tell what wrong i am doing10921-nofkey.jpg

10931-nofkey.jpg

10749-recordattendance.jpg

Azure SQL Edge
Azure SQL Edge
An Azure service that provides a small-footprint, edge-optimized data engine with built-in artificial intelligence. Previously known as Azure SQL Database Edge.
45 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,316 Reputation points
    2020-06-30T03:02:14.093+00:00

    Please specify a Many to One replationship between AttendanceRecord entity and Users

    public class AttendanceRecord
         {
             [PrimaryKey, AutoIncrement]
             public int ID { get; set; }
             [ForeignKey(typeof(Users))]
             public int UserID { get; set; }
             public DateTime InDate { get; set; }
             public double InLatitude { get; set; }
             public double InLongitude { get; set; }
             public DateTime OutDate { get; set; }
             public double OutLatitude { get; set; }
             public double OutLongitude { get; set; }
             public bool IsIsSynchronize { get; set;}
    
             [ManyToOne]      // Many to one relationship with Users
             public Users Users { get; set; }
    
         }
    

    Also, run the below command and check if ForeignKey support is enabled on the SQLite instance you are using.

    sqlite> PRAGMA foreign_keys;
    

    If not enabled, you will have to enable it manually by issuing the below command

    sqlite> PRAGMA foreign_keys = ON;
    

    --
    If you found this Answer helpful, please “Accept Answer” and Up-Vote for the same which might be beneficial to other community members reading this thread.

    0 comments No comments