如何让ASP.NET Core+Angular应用引用带EDMX的.NET类库(不改动现有MVC项目)
Hey there, let's break this down step by step so you can reuse your existing ADO.NET/EDMX project without touching your existing MVC app or forcing changes to other systems. The core idea is to turn your EDMX project into a shared class library that both your existing MVC app and new Core app can reference—no need to regenerate EF models (which caused your earlier headache).
Step 1: Prepare Your EDMX Project for Sharing
First, make sure your EDMX project is set up as a class library (if it isn't already):
- Right-click the EDMX project → Properties → Application tab.
- If it's targeting .NET Framework 4.x, upgrade it to .NET Standard 2.0 (this is safe because .NET Framework 4.6.1+ and all modern .NET Core versions support .NET Standard 2.0—your existing MVC app will keep working without any changes).
- Save the changes and rebuild the EDMX project.
Step 2: Reference the EDMX Library in Your New ASP.NET Core Project
- Open your solution with the new Angular + ASP.NET Core app.
- Right-click the Core project → Add → Project Reference.
- Select your EDMX class library from the list and click OK.
Step 3: Configure Connection Strings & DbContext
Next, set up your Core app to use the existing EDMX DbContext:
- In your Core project's
appsettings.json, add the same connection string your MVC app uses (copy it from your MVC app'sweb.configto avoid typos):
"ConnectionStrings": { "YourDbContextName": "Server=your-db-server;Database=your-db-name;Trusted_Connection=True;TrustServerCertificate=True;" }
- Update your EDMX's DbContext to support .NET Core's dependency injection (this won't break your MVC app):
- Open the
.ttfile associated with your EDMX (e.g.,YourDbContext.tt). - Find the constructor section and modify it to include a constructor that accepts
DbContextOptions:
- Open the
public <#=code.Escape(container)#>(DbContextOptions<<#=code.Escape(container)#>> options) : base(options) { }
- Rebuild the EDMX project—your MVC app can still use the existing无参 constructor, while the Core app will use this new one.
- Register the DbContext in your Core project's
Program.cs:
builder.Services.AddDbContext<YourEDMXDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("YourDbContextName")));
Step 4: Use the EDMX to Fetch User Data
Now you can access user data in your Core app, either directly via the DbContext or by wrapping it in a service for better separation:
Option A: Direct DbContext Access in Controllers
Create an API controller to expose user data to your Angular frontend:
[ApiController] [Route("api/[controller]")] public class UsersController : ControllerBase { private readonly YourEDMXDbContext _dbContext; public UsersController(YourEDMXDbContext dbContext) { _dbContext = dbContext; } [HttpGet] public IActionResult GetAllUsers() { var users = _dbContext.Users.ToList(); return Ok(users); } }
Option B: Wrap in a Service (Recommended)
For cleaner code and better reusability, add a service to your EDMX class library:
public class UserService { private readonly YourEDMXDbContext _dbContext; public UserService(YourEDMXDbContext dbContext) { _dbContext = dbContext; } public List<User> GetAllUsers() { return _dbContext.Users.ToList(); } // Add other user-related methods here (e.g., GetUserById, UpdateUser) }
Then register the service in Program.cs:
builder.Services.AddScoped<UserService>();
And use it in your controller:
[ApiController] [Route("api/[controller]")] public class UsersController : ControllerBase { private readonly UserService _userService; public UsersController(UserService userService) { _userService = userService; } [HttpGet] public IActionResult GetAllUsers() { var users = _userService.GetAllUsers(); return Ok(users); } }
Troubleshooting Common Issues
- Circular Reference Serialization Errors: If you get JSON serialization issues with related entities, configure your Core app's JSON options in
Program.cs:
builder.Services.AddControllersWithViews() .AddJsonOptions(options => { options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; });
- Framework Compatibility: If you can't upgrade your EDMX project to .NET Standard, you can target .NET Framework 4.x in your Core app (but this is less ideal for long-term maintenance).
This approach keeps your existing MVC app untouched, lets your new Angular/Core app reuse the existing EDMX database layer, and avoids the need to regenerate EF models (which was causing cross-app changes before).
内容的提问来源于stack exchange,提问作者Bartvandee

