Connect AutoCAD to database using Entity Framework Core (.Net 8.0)

Connect AutoCAD to database using Entity Framework Core (.Net 8.0)

mdoyon1
Participant Participant
1,086 Views
8 Replies
Message 1 of 9

Connect AutoCAD to database using Entity Framework Core (.Net 8.0)

mdoyon1
Participant
Participant

I have been spending the past couple of days to connect a database to AutoCAD in order to store attribute data. I used EF6 for AutoCAD 2023 (.Net Framework 4.8) but it gets more complicated with the migration to .Net Core.

 

The error happens when creating a ApplicationDbContext and some file dll cannot be loaded.

 

Error message when trying to create a database context: 
System.IO.FileLoadException: 'Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=9.0.0.0,

mdoyon1_0-1734835691207.png

 

There are only two files in the project for now:
Class1.cs:

using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: ExtensionApplication(typeof(DatabaseAutoCAD.ExtensionApplication))]
[assembly: CommandClass(typeof(DatabaseAutoCAD.Class1))]


namespace DatabaseAutoCAD
{
    public class ExtensionApplication : IExtensionApplication
    {
        public void Initialize()
        {

        }

        public void Terminate()
        {
        }
    }
    public class Class1
    {
        [CommandMethod("CreateProduct")]
        public void CreateProduct()
        {
            using (var db = new ApplicationDbContext())
            {
                db.Database.EnsureCreated();

                var product = new Product
                {
                    Name = "Product 1",
                    Description = "Description 1"
                };

                db.Products.Add(product);
                db.SaveChanges();
            }
        }

    }
}

entities.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace DatabaseAutoCAD
{
    public class Product
    {
        [Key] // Primary Key
        public int Id { get; set; }

        public string Name { get; set; } = string.Empty;

        public string? Description { get; set; } // Optional property
    }

    public class ApplicationDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; } = null!;

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public ApplicationDbContext() : base()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=app.db"); // Replace with your connection string
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Fluent API configurations (optional)
            modelBuilder.Entity<Product>()
                .HasIndex(p => p.Name)
                .IsUnique(); // Ensure the product name is unique
        }
    }
}

 

I added the following statement in the project settings in order to copy the file assemblies to the output directory:

<CopyLocalLockFileAssemblies>True</CopyLocalLockFileAssemblies>

The dll is now present in the folder when the build is completed.

 

Any help would be very much appreciated! Thanks

 

0 Likes
Accepted solutions (1)
1,087 Views
8 Replies
Replies (8)
Message 2 of 9

norman.yuan
Mentor
Mentor
Accepted solution

Since Acad2025 uses .NET 8, you need also upgrade the Entity Framework from 6.x to Miscrosoft.EntityFrameworkCore. It looks to me that you are still using Microsoft.EntityFramework, not Microsoft.EntityFrameworkCore, hence the error. When you install Microsoft.EntityFrameworkCore, Microsoft.Extensions.DepedencyInkection.Abstractions in included in the Nuget package.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 9

mdoyon1
Participant
Participant

Thank you!

0 Likes
Message 4 of 9

brand
Contributor
Contributor

I'm running into this problem as well. I am referencing EntityFrameworkCore.

brand_0-1740498770999.png

I've also included the CopyLocalLockFileAssemblies element in my .csproj file, and Microsoft.Extensions.DependencyInjection.Abstractions.dll appears in the bin folder when I build the project.

brand_1-1740498819684.png

I even went so far as copying Microsoft.Extensions.DependencyInjection.Abstractions.dll into C:\Program Files\Autodesk\Autocad 2025 so that it would be adjacent to acad.exe (this worked previously to solve an issue with another library). But not in this case, I still get the error.

brand_2-1740498990182.png

 

 

0 Likes
Message 5 of 9

ecervantesUSZDX
Observer
Observer

Did you find any solution? I'm still getting the same error

0 Likes
Message 6 of 9

brand
Contributor
Contributor

Nope, still stuck and frustrated. Have tried EF Core versions 9.0.0 to 9.0.3, none have worked.

0 Likes
Message 7 of 9

kerry_w_brown
Advisor
Advisor

@brand 

 

Have you tried one of the version 8 builds of EntityFrameworkCore ?

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 8 of 9

brand
Contributor
Contributor

I noticed there's a newly released 9.0.4 version out, which I tried this morning. Same problem.

 

Yes, I have tried the v8 builds. These have dependencies on components that aren't built on .NET 8. With EF 8.0.15, I don't get the Microsoft.Extensions.DependencyInjection.Abstractions error, but I get this instead.

 

brand_0-1744212626797.png

 

0 Likes
Message 9 of 9

brand
Contributor
Contributor

So I decided to try Dapper just to get a **bleeping** connection to the database working from inside Autocad 2025. Via NuGet I added Dapper 2.1.66 and Microsoft.Data.SqlClient 6.0.1. Revised my test query to use a SqlConnection and Dapper extension methods, not too difficult. Loaded up in Autocad and...

 

brand_0-1744231165106.png

Uggh! Did some digging and found a suggestion that you need to add <RuntimeIdentifier>win-x64</RuntimeIdentifier> element to the .csproj file. Did that, which now builds in the Debug\net8.0-windows\win-x64 folder (watch for that during NETLOAD). Ran again and finally got past the Microsoft.Data.SqlClient problem! Then ran into this:

 

brand_1-1744231361729.png

The database I'm connecting to isn't one I use often and I was trying to use a trusted connection. I had to add TrustServerCertificate=True to my connection string.

 

brand_2-1744231474921.png

And finally I'm pulling data from a database!

 

brand_3-1744231555204.png

 

References:

https://github.com/dotnet/SqlClient/issues/2030
Look for IzharAzati's comment 3/4 of the way through this thread...who was trying to get his Autocad plugin to work :/.
Welcome To Learn Dapper ORM - A Dapper Tutorial for C# and .NET Core