.NET 6 + EF-Core

2,268 views
Skip to first unread message

Mark

unread,
Jul 12, 2022, 5:13:28 AM7/12/22
to Excel-DNA
Hi All,

I'm trying to migrate a project to .NET 6 and at the same time add EF Core support, however when I try and access any entities via the  context object I get the following error:

System.PlatformNotSupportedException: 'Strings.PlatformNotSupported_DataSqlClient'

Clearly I'm missing something or doing something stupid. I also know the code / EF works fine via my desktop application consuming the same EF Core project.

Are there any documentation / examples I can follow to help me consume an Entity Framework project within my Excel DNA project?

Thanks
Mark

Govert van Drimmelen

unread,
Jul 12, 2022, 7:23:17 AM7/12/22
to exce...@googlegroups.com

--------------------------------------------------

Excel-DNA is now registered on GitHub Sponsors.

With a small monthly contribution you encourage further development and support.

--------------------------------------------------

 

Hi Mark,

 

I presume you are running into an issue where the SqlClient is not locating and loading the right additional .dll libraries it requires.

Then it falls back to am implementation that throws this exception.

See

Frequently Asked Questions · dotnet/SqlClient Wiki (github.com)

 

You might want to see how a net6.0-windows console application ends up looking in the output directory (or directories under it).

If you have the right libraries in the Excel-DNA add-in’s output directory, but still get this error, then maybe you need to locate and preload some library yourself.

 

If you’re still stuck, it would help to put a little self-contained sample add-in on GitHub that exhibits the problem and we can use as a starting point.

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/c8cf6d06-bffc-4a15-8609-31dd020976a2n%40googlegroups.com.

Mark

unread,
Jul 12, 2022, 2:30:05 PM7/12/22
to Excel-DNA
Thanks for your advice Govert,

You might have to bare with me here for a bit .. chances are I'm being an idiot as I try and learn new tech, however:

I created a new console app -  Everything worked as expected I was able to query my database without issue.   After  using NuGet to install Excel DNA and running the code within the plugin / ribbon it reverts to the error reported earlier.

So it appears some part of installing Excel DNA is causing the issues. I'll check the DLLs next but feel something might be happening during the Excel DNA process that is causing these issues.

Thanks
Mark

Henrik Teneberg

unread,
Aug 17, 2022, 7:31:25 AM8/17/22
to Excel-DNA
Hi,

I have a similar problem with .NET6 and System.Data.OleDb. I get the same kind of behavior (OleDb stops working when inside an Excel DNA Addin, but works fine on its own). I have tried pointing directly to the .NET6 dll instead of going through the Nuget smartness, without success.

Sample code which returns "System.Data.OleDb is not supported on this platform.":
using ExcelDna.Integration;
using System.Data.OleDb;

namespace TestExcelDNAAndSystemDataOleDb
{
    public static class MyFunctions
    {
        [ExcelFunction(Description = "My first .NET function")]
        public static string HelloDna(string name)
        {
            try
            {
                var myConn = new OleDbConnection("dummy");
                return "Hello " + name;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}

This is my project SDK:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="ExcelDna.AddIn" Version="1.6.0-preview5" />
    <PackageReference Include="System.Data.OleDb" Version="6.0.0" />
  </ItemGroup>

</Project>

Govert van Drimmelen

unread,
Aug 17, 2022, 6:57:47 PM8/17/22
to exce...@googlegroups.com

Hi Henrik,

 

Can you try by copying the 6 files under

    bin\debug\net6.0-windows\runtimes\win\lib\net6.0

into the directory

    bin\debug\net6.0-windows

 

This will overwrite the default implementation of EFCore (which is giving you the problematic message) with the windows version of the files that you need.

After that I get a new error message:

    Format of the initialization string does not conform to specification starting at index 0.

 

This makes sense given your test and should allow you to get a bit further.

Henrik Teneberg

unread,
Aug 24, 2022, 2:50:12 AM8/24/22
to Excel-DNA
Hi Govert,

Thanks for the tip, it worked!

Cheers,
Henrik

Glayson Patricio

unread,
Aug 29, 2022, 5:29:12 PM8/29/22
to Excel-DNA
Hi Govert, Hi All,

I am NOT using EF-CORE. 
I am using a raw DataBase Connection with Microsoft.Data.SqlClient, and i am receiving the same error message:  "Microsoft.Data.SqlClient is not supported on this platform."

I tryed copying the files under "AddIn\bin\Debug\net6.0-windows\runtimes\win\lib\net6.0" (I have only two files)
  - System.Data.OleDb.dll
  - System.Diagnostics.EventLog.Messages.dll
To "AddIn\bin\Debug\net6.0-windows"

But I can't get it to work.

Can someone give me a clue?

Glayson

Govert van Drimmelen

unread,
Aug 30, 2022, 8:53:34 AM8/30/22
to exce...@googlegroups.com

Hi Glayson,

 

After building, you need to copy these two files into your output directory (which would be something like bin\debug\net6.0-windows\)

  • Microsoft.Data.SqlClient.dll from bin\debug\net6.0-windows\runtimes\win\lib\newcoreapp3.1\
  • Microsoft.Data.SqlClient.SNI.dll from bin\debug\net6.0-windows\runtimes\win-x64\native\

 

Every time you rebuild, the first one will be overwritten, so you’ll have to make some post-build plan to automate this.

Glayson Patricio

unread,
Aug 30, 2022, 10:55:20 PM8/30/22
to Excel-DNA
Hi Govert,

Thankyou very much. It Worked (while debugguing on visual studio)

To automate the post build plan, i wrote that on the project file
    <Target Name="PostBuild" AfterTargets="PostBuildEvent">
        <Exec Command="copy $(OutDir)\runtimes\win-x64\native\Microsoft.Data.SqlClient.SNI.dll $(OutDir)" />
        <Exec Command="copy $(OutDir)\runtimes\win\lib\netcoreapp3.1\Microsoft.Data.SqlClient.dll $(OutDir)" />
    </Target>

But i am still getting the same error when i open an excel with the addin previously installed 
(without any kind of connection to visual studio)

Any Tips ?
Maybe I need to copy more files ?

Glayson

Enver TOLEUOV

unread,
Aug 31, 2022, 8:07:44 AM8/31/22
to Excel-DNA
Hi Govert,

I am new to ExcelDNA.
When I trying to db.SaveChanges(), Excel is throwing Exception "The type initializer for Microsoft.Data.SQLite.SQLiteConnection threw and exception"

My code is:
1) ApplicationContext.cs --->

using Microsoft.EntityFrameworkCore;

public class ApplicationContext : DbContext
{
    public DbSet<User> Users { get; set; } = null!;

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
       optionsBuilder.UseSqlite("Data Source=test.db");
    }
}

public class User
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public int Age { get; set; }
}

2) RibbonControler.cs -----> 

using System.Runtime.InteropServices;
using ExcelDna.Integration.CustomUI;

namespace Ribbon
{
    [ComVisible(true)]
    public class RibbonController : ExcelRibbon
    {
        public override string GetCustomUI(string RibbonID)
        {
            return @"
      <customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui'>
            <ribbon>
                <tabs>
                    <tab id='tab1' label='PlannerTools'>                        
                        <group id='parse_XER' label='Parse XER'>    
                            <button imageMso='CostResourceInsert'
                                    id='button_parse_XER_sqlite'
                                    label='Parse XER SQLite'
                                    onAction='OnButtonPressed_Parse_XER_SQLite'
                                    supertip='Parses content of XER file'
                                    />                        
                        </group>
                    </tab>
                </tabs>
            </ribbon>
    </customUI>";
        }        

        public void OnButtonPressed_Parse_XER_SQLite(IRibbonControl control)
        {
            // Not working, throwing Exceptions;
            using (ApplicationContext db = new ApplicationContext())
            {
               
                User tom = new User { Name = "Tom", Age = 33 };
                User alice = new User { Name = "Alice", Age = 26 };
               
                db.Users.Add(tom);
                db.Users.Add(alice);
                db.SaveChanges();
               
            }

            return;
        }
    }
}

Glayson Patricio

unread,
Aug 31, 2022, 10:57:49 AM8/31/22
to Excel-DNA
About my comment on the last message: 

"But i am still getting the same error when i open an excel with the addin previously installed 
(without any kind of connection to visual studio)

Any Tips ?
Maybe I need to copy more files ?"

I am sorry ... the error is not the same.
I will investigate the new error now!

thanks for the help

Reply all
Reply to author
Forward
0 new messages