C# 在使用EntityFramework核心连接不同类型的列时,如何急切地加载导航属性?
我们有一个遗留数据库,我们需要以只读方式与该数据库交互以获取一些数据(我们不能更改其模式) 有些表不使用FKs相互关联,而是使用列的值,不幸的是,依赖表上的主键和相关列具有不同的数据类型(ex char和nvarchar) 使用实体框架核心,我编写了如下映射: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
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()我已经试过了。供应商返回的nullIs供应商与CatalogeItems映射时无效。你能在DB中检查一下你是否得到了正确的列和关系表SYEA吗?正如我在问题中提到的,当我得到一个目录时,我可以找到相关的供应商
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();
}
}