代表EF中的单列表? [英] Represent a single-rowed table in EF?

查看:98
本文介绍了代表EF中的单列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个配置表,它只包含一行。

  ConfirmedScheduleColor | OverlappedScheduleColor | ColN 

目前,我正在检索如下配置:

  var db = new SchedulingDbContext(); 
var config = db.Configurations.FirstOrDefault();

目前工作正常,我可以访问我的配置和所有。事情是,代码看起来很尴尬,因为我正在访问配置 DbSet ,好像它包含许多记录( FirstOrDefault());实际上它只包含一个记录。我想访问我的配置,就像我正在访问静态对象。如何在EF中执行此操作?

解决方案

您可以简单地添加一个属性到您的 DbContext 返回 Configurations.FirstOrDefault()并私有化 DbSet

  public class SchedulingDbContext:DbContext 
{
private DbSet< Configuration>配置{get;组; }

public配置
{
get
{
return Configurations.FirstOrDefault();
}
}
}


I have a configuration table in my database and it contains exactly one row.

ConfirmedScheduleColor | OverlappedScheduleColor | ColN

Currently, I'm retrieving the configuration like this:

var db = new SchedulingDbContext();
var config = db.Configurations.FirstOrDefault();

It's currently working fine and I can access my configurations and all. The thing is, the code looks awkward since I'm accessing the Configurations DbSet as if it contains many records (FirstOrDefault()); although actually, it contains only one record. I want to access my configurations like I'm accessing a static object. How to do that in EF?

解决方案

You could simply add a property to your DbContext that returns Configurations.FirstOrDefault() and privatize the DbSet:

public class SchedulingDbContext : DbContext
{
    private DbSet<Configuration> Configurations { get; set; }

    public Configuration Configuration
    {
        get
        {
            return Configurations.FirstOrDefault();
        }
    }
}

这篇关于代表EF中的单列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆