You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:System.InvalidOperationException——Customer实体不在当前上下文模型中

解决System.InvalidOperationException: The entity type Customer is not part of the model for the current context.

Alright, let's break down how to fix this frustrating error you're seeing when trying to fetch your Customer list. Based on the code you shared, here are the most likely culprits and actionable fixes:

1. Fix the Trailing Space in Your Connection String Name

First, look at your context constructor:

public StoreSalesHandlingNewEntities1() : base("name=StoreSalesHandlingNewEntities1 ")

Notice the trailing space after StoreSalesHandlingNewEntities1? When EF tries to look up the connection string in your config, it's searching for a name with that extra space—something that almost certainly doesn't exist. Remove the space immediately:

public StoreSalesHandlingNewEntities1() : base("name=StoreSalesHandlingNewEntities1")

2. Add a Primary Key to Your Customer Entity

EF requires every entity to have a primary key to map it correctly to the database table. Your current Customer class has no property marked as a primary key—this is a critical oversight.

Assuming IDNumber is the primary key in your SQL Customer table, add the [Key] attribute (you'll need to reference System.ComponentModel.DataAnnotations):

using System.ComponentModel.DataAnnotations;

public partial class Customer
{
    [Key] // Mark this as the primary key
    public string IDNumber { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string EmailAddress { get; set; }
    public string HomeAddress { get; set; }
    public int MobileNumber { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public override string ToString()
    {
        return string.Format("Name: {0}, Surname: {1}, ID Number: {2}, Email: {3}, Address: {4}, Mobile: {5}, Username: {6}, Password: {7}", Name, Surname, IDNumber, EmailAddress, HomeAddress, MobileNumber, Username, Password);
    }
}

If your table uses a different primary key (like an auto-incrementing CustomerId), add that property and mark it with [Key] instead.

3. Fix the OnModelCreating Method

Your current OnModelCreating method throws an UnintentionalCodeFirstException, which tells EF you don't want to use Code First mode—but if you're connecting to an existing database (Database First), this method should contain entity mapping configurations, not an error.

  • If you wrote this context manually: Delete the throw statement. You can leave the method empty (EF will use convention-based mapping) or add explicit table/column mappings if needed.
  • If you intended to use Database First: Regenerate your context and entities using the EF Designer (right-click your project → Add → New Item → ADO.NET Entity Data Model → EF Designer from Database). This will automatically generate the correct OnModelCreating code and entity mappings.

4. Verify Your Connection String Configuration

Double-check your App.config or Web.config to ensure:

  • The connection string name matches exactly (no extra spaces) what's in your context constructor.
  • The connection string points to the correct SQL Server instance and database that contains the Customer table.
  • The database table name matches your entity name (EF is case-insensitive by default, but it's good to confirm no typos exist).

Quick Test After Fixes

Once you've applied the above changes, run your code again. The db.Customer.ToList() call should now correctly map to your SQL table and return the customer list without errors.

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:46:48