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,
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
Solved! Go to Solution.
Solved by norman.yuan. Go to 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.
Can't find what you're looking for? Ask the community or share your knowledge.