Xamarin.Forms using SQLite.Net.Async (the NUGET part) - c#

I'm trying to make a connection to a SQLite database using Visual Studio 2015 the target platform is Android.
I've found this SO question but I can not get it to work. My problem is the part of installing packagde.
I want to have my database stuff placed in a Class Library so I've created a new Application, and added a Class Library to the solution.
To install SQLite I've written the following in the packagde manager console:
Install-Package SQLite.Net.Platform.XamarinAndroid -Version 2.3.0
But installs SQLLite into my application, not my Class Library.
My Class Library is very very simple:
public class SQLiteDataService
{
private SQLiteAsyncConnection connectionFlexCheckDemo;
private ISQLitePlatform sqlitePlatform;
private string FlexCheckDBNameDemo = "FlexCheckDemo.db";
private void Test()
{
var platform = new SQLitePlatformAndroid();
}
}
but it doesn't compile:
Error CS0234 The type or namespace name 'Net' does not exist in the
namespace 'SQLite' (are you missing an assembly reference?) ...
What am I doing wrong? And more important how to make it work.

You will have to install the Nuget or the dll to reference the SQLite-Async in your Shared or PCL project so that you can write your common code there like having common CRUD operations or having Unit Of Work coupled with Repository pattern. A simple example can be found here.
Then in your platform specific projects you need to have the nuget installed as well, which will get resolved and install the corresponding version based on your platform. I don't think there is a nuget available for WP, so you might have to look for other choices.
This is the nuget package for SQLite.Net.Async PCL

Related

Class Library, Entity Framework Code First & Microsoft.SqlServer.Types

I am having a few issues trying to get code-first Entity Framework 6.0 to build a migration with Microsoft.SQLServer.Types. I still keep getting:
Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found.
Everywhere I look they keep talking about running:
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
or similar in the global.asax. I need to run this in a class library which handles the data layer of my application, so there isn't a startup event.
I have put in
[assembly: PreApplicationStartMethod(typeof(Test.Startup), "Start")]
namespace Test
{
public class Startup
{
public static void Start()
{
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
}
}
}
But that has not had any effect. Anyone got any other ideas?
Thanks
Chris
Ooh! I just had this problem last week!
Let's start by clarifying a few things. This error is caused by the lack of having an assembly to manage the Sql Server Spatial types. You can solve this issue by either:
Installing SQL Server on the machine
Taking a dependency on a NuGet package.
Typically, going with the NuGet package is easier, so I'm going to explain that.
The version of the NuGet package that you get depends on the version of Sql Server that you have running. My version of Sql Server is 11.0.6020.0, so I installed the 11.0.2 version of the NuGet package. This is super important to match the versions.
There are three assemblies that you'll need.
Microsoft.SqlServer.Types.dll
msvcr<Version>.dll
SqlServerSpatial<Version>.dll
Microsoft.SqlServer.Types.dll is a managed assembly, while the other two are native assemblies.
If you install the correct version of the NuGet package, then all of these should be handled for you.
After you install the NuGet package, it'll open a readme, and it gives the example on how to load the native assemblies by using the Loader class.
Code sample:
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
Lastly, you'll need to override the SqlServerTypesAssemblyName property in the SqlProviderServices static class. Note that this is only available in a later version of EntityFramework. I'm using EntityFramework 6.1.3. In my research, others were saying that this wasn't available in earlier versions.
Once again, I was using version 11.0.2 to match my Sql Server version, so I used the following code:
SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
If you're using a newer version of Sql Server, you should be able to replace the 11 with 12, 13, or 14 without issues.
If it's not working, here's a quick checklist:
Ensure that Microsoft.SqlServer.Types.dll is referenced.
Ensure that the native assemblies are copied to your bin directory under the ..\SqlServerTypes\<Platform>\ folder. There should be two.
Ensure that you're setting calling the LoadNativeAssemblies and setting the SqlServerTypesAssemblyName before any database operations, including creating your DbContext instance.
If your DbContext is in a separate assembly than the executing assembly, you may need to reference the Microsoft.SqlServer.Types assembly. It may not be copied over via the build/reference process.

Namespace not recognized for project reference to core framework project from full framework project

Easiest way to describe the problem is by providing steps to reproduce.
Using the release bits (VS 2015 Update 3 and .NET Core 1.0), create solution with two projects as follows:
Create .NET Core class library.
Change "netstandard1.6" to "netstandard1.4" in project.json.
Create Full Framework console application.
Change target framework in project properties to "4.6.1".
This versions both projects to the same .NET platform standard.
From console app, add project reference to class library project.
In the console app code, reference a class in the class library project, e.g.:
static void Main(string[] args)
{
var x = new ClassLibrary1.Class1();
}
Produces the following error message:
The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?)
looks like this is an issue with the xproj project reference. If you compile the code and reference the dll specifically it should work. This has been discussed at length here
From what I can tell, we are stuck waiting for the conversion of xproj to csproj, which should resolve the issue.

Assembly reference error (CS0234) when using System.Security.Cryptography in Xamarin Studio 6.1 on Mac OS X

Running Xamarin Studio (Community) 6.1 (build 4963) on OS X El Capitan 10.11.5, I am trying to use RNGCryptoService in my solution (targets iOS and Android) for which I need to use the namespace System.Security.Cryptography. However, it seems like using System.Security.Cryptography; and then calling RNGCryptoServiceProvider random = new RNGCryptoServiceProvider(); somewhere in my code gives me the error.
Error CS0234: The type or namespace name 'Cryptography' does not exist in the namespace 'System.Security'. Are you missing an assembly reference? (CS0234)
I tried to look for the System.Security.Cryptography package in the NuGet repositories (including the pre-release versions) but I did not find anything.
What am I missing? Where can I find the assembly reference?
You need the System.Security.Cryptography.Algorithms package to use RNGCryptoServiceProvider. Install that with NuGet v 3.4 or later:
> PM Install-Package System.Security.Cryptography.Algorithms
You may also need to modify your project.json file to make this package a dependency of dotnet5.4 and not a global dependency to stop the compiler from complaining about duplicate class declarations.
Follow up: It looks like System.Security.dll is not supported on Xamarin.iOS or Android. As an alternative, you might have some luck with Jeffrey Stedfast's fork of the Bouncy Castle cryptography library bc-csharp for use with Xamarin.Android and Xamarin.iOS.
Another option might be to use the PCLCrypto library with this helper class and workaround for NuGet.

How to get MVVM Light Messenger working with a PCL

I am currently reorganizing my WP8 project in order to have a Portable Class Library, which could result in a W8 app.
I am using MVVMLight and have the following code:
In the Portable Class Library:
namespace MyApp.Shared.Messages
{
public class MyItemSelectedMessage : MessageBase
{
public MyItemSelectedMessage(MyItem item)
{
Item = item;
}
public MyItem Item { get; set; }
}
}
In my WP8 app, I still do have the ViewModels (because of Telerik and some other stuff which I cannot outsource to the PCL), where I use the following code to register for the message:
Messenger.Default.Register<MyItemSelectedMessage>(this, msg => SelectedItem = msg.Item);
The code worked fine before but now VS gives me the following error in the WP8 app at the given line:
The type 'GalaSoft.MvvmLight.Messaging.MessageBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'GalaSoft.MvvmLight, Version=4.2.30.16997, Culture=neutral, PublicKeyToken=null'
In my WP8 project, I have installed MvvmLight (v4.2.30.23483) and in my PCL there are the MvvmLight Libraries for PCL (v4.2.30.16997), both up-to-date from Nuget. Do I need to downgrade a package to get the same build number, add the libraries manually or what can I do in order to make it work?
Thanks in advance!
You should reference the Portable.MvvmLightLibs NuGet package from both projects, and not any of the non-portable MvvmLight packages. The Portable.MvvmLightLibs package includes support for both PCLs and platform-specific libraries.

Nuget Package from a Class Library Required in Main Project?

I have created a Data Access Layer as a Class Library. This class library has a nuget package installed for my ORM. When I try to use my Class Library in my main project it complains that the main project doesn't have my ORM Nuget Package installed.
I thought that if the Class Library had that Nuget Package installed then I wouldn't need that package installed for every project that uses it.
Am I doing something wrong here or is that just the way it works?
Thanks!
UPDATE
In my DAL I have a DataRepositoryBase class that has a method called GetPaged. Under the covers this DataRepositoryBase class uses ServiceStack's ORMLite.
In my Program I have the following code:
using (DataRepositoryBase<Inventory> InvRepo = new DataRepositoryBase<Inventory>())
{
IEnumerable<Inventory> invList = InvRepo.GetPaged(i => i.Cust_ID == CustID, 0, 10);
}
My main program is complaining about not having a reference to ORMLite.
If you are referencing types in the DAL from the Main Project then you have to reference that assembly from there as well.

Categories

Resources