Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc mvc 6 ef7客户表对象在AppDbContext中为空-数据库中有数据_Asp.net Mvc_Entity Framework_Object_Null_Dbcontext - Fatal编程技术网

Asp.net mvc mvc 6 ef7客户表对象在AppDbContext中为空-数据库中有数据

Asp.net mvc mvc 6 ef7客户表对象在AppDbContext中为空-数据库中有数据,asp.net-mvc,entity-framework,object,null,dbcontext,Asp.net Mvc,Entity Framework,Object,Null,Dbcontext,当我输入URL localhost:xxx/api/Customers时,它显示为“is null”。我在调试器模式下查看变量,它显示表customers对象为null 记住表中有数据。有什么想法吗 客户类别 public class Customer { public string Id { get; set; } public string lName { get; set; } } appsettings.json { "Data": { "DefaultConnect

当我输入URL localhost:xxx/api/Customers时,它显示为“is null”。我在调试器模式下查看变量,它显示表customers对象为null

记住表中有数据。有什么想法吗

客户类别

public class Customer
{
    public string Id { get; set; }
    public string lName { get; set; }
}
appsettings.json

{
"Data": {
"DefaultConnection": {
  "ConnectionString": "Server=.\\sqlexpress;Database=EF7;Trusted_Connection=True;"
}
}
AppDbContext

public class AppDbContext : DbContext
{
    public DbSet<Customer> Customers;
    protected override void OnModelCreating(ModelBuilder Builder)
    {
        Builder.Entity<Customer>().HasKey(c => c.Id);
        base.OnModelCreating(Builder);
    }
 }
Startup.cs

public class Startup
{
    IConfigurationRoot Configuration;
    public Startup(IHostingEnvironment env)
    {
        var Builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = Builder.Build();
    }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddEntityFramework().AddSqlServer().AddDbContext<AppDbContext>(o=>o.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseMvc();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
公共类启动
{
i配置根配置;
公共启动(IHostingEnvironment环境)
{
var Builder=new ConfigurationBuilder()
.AddJsonFile(“appsettings.json”)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
Configuration=Builder.Build();
}
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddEntityFramework();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序)
{
app.UseIISPlatformHandler();
app.UseMvc();
}
//应用程序的入口点。
publicstaticvoidmain(字符串[]args)=>WebApplication.Run(args);
}

没关系,我的数据库集是一个字段而不是属性。所以我刚改为DbSet Customer{get;set}

public class Startup
{
    IConfigurationRoot Configuration;
    public Startup(IHostingEnvironment env)
    {
        var Builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = Builder.Build();
    }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddEntityFramework().AddSqlServer().AddDbContext<AppDbContext>(o=>o.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseMvc();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}