Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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
C# 在使用EntityFramework核心连接不同类型的列时,如何急切地加载导航属性?_C#_Orm_Entity Framework Core - Fatal编程技术网

C# 在使用EntityFramework核心连接不同类型的列时,如何急切地加载导航属性?

C# 在使用EntityFramework核心连接不同类型的列时,如何急切地加载导航属性?,c#,orm,entity-framework-core,C#,Orm,Entity Framework Core,我们有一个遗留数据库,我们需要以只读方式与该数据库交互以获取一些数据(我们不能更改其模式) 有些表不使用FKs相互关联,而是使用列的值,不幸的是,依赖表上的主键和相关列具有不同的数据类型(ex char和nvarchar) 使用实体框架核心,我编写了如下映射: EntityTypeBuilder<Supplier>.HasMany(x => x.CatalogueItems) .WithOne(x => x.Supplier) .HasFo

我们有一个遗留数据库,我们需要以只读方式与该数据库交互以获取一些数据(我们不能更改其模式)

有些表不使用FKs相互关联,而是使用列的值,不幸的是,依赖表上的主键和相关列具有不同的数据类型(ex char和nvarchar)

使用实体框架核心,我编写了如下映射:

EntityTypeBuilder<Supplier>.HasMany(x => x.CatalogueItems)
        .WithOne(x => x.Supplier)
        .HasForeignKey(x => x.Vendor);
我将供应商设置为空,当我使用投影写入时,我将获得供应商数据:

dbContext.CatalogueItems.Select(x => new {
                               x.ItemCode,
                               x.Supplier 
                        }).FirstOrDefault();
您能告诉我如何使用Include调整它以获得导航属性吗

编辑:

Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MyDBContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));

        services.AddIdentityCore<User>()
        .AddEntityFrameworkStores<MyDBContext>()
        .AddDefaultTokenProviders();


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // Add application services.

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
            app.UseHttpsRedirection();
        }

        app.UseMvc();
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“MyConnection”));
services.AddIdentityCore()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//添加应用程序服务。
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseHsts();
app.UseHttpsRedirection();
}
app.UseMvc();
}
}

您可以共享startup.cs吗还可以尝试
var item=(从dbContext.catalogeItems.Include(s=>s.Supplier)中的obj选择obj.SingleOrDefault()public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MyDBContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));

        services.AddIdentityCore<User>()
        .AddEntityFrameworkStores<MyDBContext>()
        .AddDefaultTokenProviders();


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // Add application services.

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
            app.UseHttpsRedirection();
        }

        app.UseMvc();
    }
}