Skip to content

Commit

Permalink
Db service changed from EntityFramework to M.D.SqlClient. Works on Wi…
Browse files Browse the repository at this point in the history
…n and Android. Needed workaround from

dotnet/SqlClient#1656 (comment)
  • Loading branch information
Bish0p3 committed Feb 11, 2024
1 parent a8c69dc commit caa0717
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 111 deletions.
40 changes: 0 additions & 40 deletions Magazynek/Data/ApplicationDbContext.cs

This file was deleted.

13 changes: 10 additions & 3 deletions Magazynek/Magazynek.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net7.0-ios;net7.0-maccatalyst;net7.0-android</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
Expand Down Expand Up @@ -30,6 +29,10 @@
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
Expand All @@ -54,7 +57,7 @@

<ItemGroup>
<PackageReference Include="EPPlus" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.15" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>

Expand All @@ -79,4 +82,8 @@
</MauiXaml>
</ItemGroup>

<ItemGroup>
<Folder Include="Data\" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions Magazynek/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
// DB ERRORS WORKAROUND
#if ANDROID && DEBUG
Platforms.Android.DangerousTrustProvider.Register();
#endif
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
Expand Down
2 changes: 2 additions & 0 deletions Magazynek/Models/AsortymentyModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Magazynek.Models
{
[Table("ModelDanychContainer.Asortymenty")] // Specify the table name here
public class AsortymentyModel
{
public int Id { get; set; }
Expand Down
50 changes: 50 additions & 0 deletions Magazynek/Platforms/Android/DangerousTrustProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Java.Net;
using Java.Security;
using Java.Security.Cert;
using Javax.Net.Ssl;

namespace Magazynek.Platforms.Android
{
internal class DangerousTrustProvider : Provider
{
private const string TRUST_PROVIDER_ALG = "DangerousTrustAlgorithm";
private const string TRUST_PROVIDER_ID = "DangerousTrustProvider";

public DangerousTrustProvider() : base(TRUST_PROVIDER_ID, 1, string.Empty)
{
var key = "TrustManagerFactory." + DangerousTrustManagerFactory.GetAlgorithm();
var val = Java.Lang.Class.FromType(typeof(DangerousTrustManagerFactory)).Name;
Put(key, val);
}

public static void Register()
{
Provider registered = Security.GetProvider(TRUST_PROVIDER_ID);
if (null == registered)
{
Security.InsertProviderAt(new DangerousTrustProvider(), 1);
Security.SetProperty("ssl.TrustManagerFactory.algorithm", TRUST_PROVIDER_ALG);
}
}

public class DangerousTrustManager : X509ExtendedTrustManager
{
public override void CheckClientTrusted(X509Certificate[] chain, string authType, Socket socket) { }
public override void CheckClientTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
public override void CheckClientTrusted(X509Certificate[] chain, string authType) { }
public override void CheckServerTrusted(X509Certificate[] chain, string authType, Socket socket) { }
public override void CheckServerTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
public override void CheckServerTrusted(X509Certificate[] chain, string authType) { }
public override X509Certificate[] GetAcceptedIssuers() => Array.Empty<X509Certificate>();
}

public class DangerousTrustManagerFactory : TrustManagerFactorySpi
{
protected override void EngineInit(IManagerFactoryParameters mgrparams) { }
protected override void EngineInit(KeyStore keystore) { }
protected override ITrustManager[] EngineGetTrustManagers() => new ITrustManager[] { new DangerousTrustManager() };
public static string GetAlgorithm() => TRUST_PROVIDER_ALG;
}
}
}
7 changes: 7 additions & 0 deletions Magazynek/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ namespace Magazynek
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
#if DEBUG
Platforms.Android.DangerousTrustProvider.Register();
#endif
}
}
}
63 changes: 63 additions & 0 deletions Magazynek/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Magazynek.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;

namespace Magazynek.Services
{
public class DatabaseService
{
public DatabaseService()
{


}
public List<AsortymentyModel> GetYourData()
{
string srvrdbname = "Nexo_demo_1";
string srvrname = "192.168.2.164";
string srvrusername = "borys";
string srvrpassword = "admin";

string sqlconn = $"Data Source={srvrname};Initial Catalog={srvrdbname};MultipleActiveResultSets=true; User Id={srvrusername};Password={srvrpassword};Persist Security Info=True;TrustServerCertificate=True;Connection Timeout=30;";

List<AsortymentyModel> data = new List<AsortymentyModel>();

try
{
using (SqlConnection connection = new SqlConnection(sqlconn))
{
connection.Open();

string sqlQuery = "SELECT Id, Nazwa FROM ModelDanychContainer.Asortymenty";
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
AsortymentyModel item = new AsortymentyModel
{
Id = reader.GetInt32(0),
Nazwa = reader.GetString(1),
// Map other properties as needed
};
data.Add(item);
}
}
}
}
}
catch (Exception ex)
{
string message = ex.Message;
}

return data;
}
}
}
34 changes: 12 additions & 22 deletions Magazynek/ViewModels/DbViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
using Magazynek.Data;
using Magazynek.Helpers;
using Magazynek.Helpers;
using Magazynek.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using Magazynek.Services;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Magazynek.ViewModels
{
public class DbViewModel : ObservableObject
{
public DbViewModel()
{

}
private readonly ApplicationDbContext _context;

public DbViewModel(ApplicationDbContext context)
{
_context = context;
}

private ObservableCollection<AsortymentyModel> _asortyment;
public ObservableCollection<AsortymentyModel> Asortyment
Expand All @@ -50,11 +34,18 @@ public ICommand ShowDataCommand

private void LoadData()
{
var dbContext = new ApplicationDbContext();
if (dbContext.IsDatabaseConnected())
DatabaseService dbservice = new DatabaseService();

if (dbservice != null)
{
//using (var _context = new ApplicationDbContext())
//{
// // Retrieve data using LINQ query
// List<AsortymentyModel> dataList = _context.Asortyment.ToList();

//}
Console.WriteLine("Database is connected!");
Asortyment = new ObservableCollection<AsortymentyModel>(_context.Asortyment.ToList());
Asortyment = new ObservableCollection<AsortymentyModel>();
}
else
{
Expand All @@ -63,7 +54,6 @@ private void LoadData()
{
new AsortymentyModel { Nazwa = "TEST"}
};

}

}
Expand Down
Loading

0 comments on commit caa0717

Please sign in to comment.