question

Nacompllo avatar image
0 Votes"
Nacompllo asked ·

Can't create tables with some reference to SQLiteNetExtensions Xamarin.Forms

I am trying to implement the SQLiteNetExtensions.Async nuget in a Xamarin.Forms project but when I try to create a table with some reference to SQLiteNetExtensions I get the following exception when trying to create a table: Could not load file or assembly 'SQLite-net, Version=1.4.118.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the specified file.

I have 3 models: class, student and enrollment:
GrzBt.png

 [Table("Classes")]
 public class Class
 {
     [PrimaryKey]
     [AutoIncrement]
     public int ClassId { get; set; }
    
     public string Title { get; set; }
    
     public string Description { get; set; }
    
     [ManyToMany(typeof(Enrollment))]
     public List<Student> Students { get; set; }
 }
    
 [Table("Students")]
 public class Student
 {
     [PrimaryKey]
     [AutoIncrement]
     public int StudentId { get; set; }
    
     public string LastName { get; set; }
    
     public string FirstName { get; set; }
    
     [ManyToMany(typeof(Enrollment))]
     public List<Class> Classes { get; set; }
 }
    
 [Table("Enrollments")]
 public class Enrollment
 {
     public int EnrollmentId { get; set; }
    
     [ForeignKey(typeof(Student))]
     public int StudentId { get; set; }
    
     [ForeignKey(typeof(Class))]
     public int ClassId { get; set; }
 }

If I remove from the models the special properties of this extension nuget such as: ForeignKey, ManyToMany the application works correctly and creates the tables and allows data to be saved but when I add any property of the extension nuget it does not even allow to create the tables. I have spent a long time trying to figure out what is happening but I am not able to find out given the few examples on the internet.

SQLiteNetExtensions.Async version 2.1.0: https://www.nuget.org/packages/SQLiteNetExtensions.Async/

Xamarin.Forms version 5.0.0.2012

I have published the project on GitHub so that it can be reviewed in its entirety and serve in the future for more users: https://github.com/nacompllo/XamarinFormsSQLiteNetExtensionsSample

dotnet-xamarinforms
· 6
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When you insert the record to the Classes table, you should update the records in the `Student and Get the object and the relationships(Enrollments table), you can refer to the following demo code.

https://github.com/media-tools/sqlite-net-extensions#read-and-write-operations

0 Votes 0 ·

Yes, I know, but that is not the question, that part is not complete because I have encountered the error that I describe in the question above, when the application starts, the tables should be created since if we remove the ForeignKey from the models and the ManyToMany relationships are created correctly, but if we keep these attributes the application crashes at startup with the following exception:
Could not load file or assembly 'SQLite-net, Version = 1.4.118.0, Culture = neutral, PublicKeyToken = null'. The system cannot find the specified file.

The repository you mention I have already viewed on many occasions but I have the important things the same and the exception that I mentioned above continues to occur.

0 Votes 0 ·

First, I test your demo, I cannot get this error(keep the Enrollment database). Then I add await SQLiteNetExtensionsAsync.Extensions.WriteOperations.InsertWithChildrenAsync(Database, student1); to insert data and var data = await SQLiteNetExtensionsAsync.Extensions.ReadOperations.GetAllWithChildrenAsync<Student>(Database); to get data.

73478-image.png


0 Votes 0 ·
image.png (44.7 KiB)
Show more comments

1 Answer

Nacompllo avatar image
0 Votes"
Nacompllo answered ·

Good news, I have managed to solve this problem, after downloading the source code of SQLite-Net Extensions from the original repository I have proceeded to update the target framework from the version .NET Standard 1.1 to .NET Standard 2.0 of the SQLiteNetExtensions project and SQLiteNetExtensionsAsync from the original repository and I have created a local nuget of those two projects which I have imported into my example project and I have uploaded this solution on my GitHub in case someone needs it in the future.

I have uploaded a commit in my GitHub repository with the solution implemented: https://github.com/nacompllo/XamarinFormsSQLiteNetExtensionsSample
I have also posted a pull request in the original repository to integrate the fix: https://bitbucket.org/twincoders/sqlite-net-extensions/pull-requests/32/fix-crash-in-uwp-for-xamarinforms

74288-sql.png
These are the two projects from the original repository that need to be updated.





sql.png (2.5 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your sharing.

0 Votes 0 ·