1

我正在使用 MVVM 和实体框架,并且有一个模型类映射到一个名为EmplTable.

在这堂课中,我有几个字段,我们将这个产品运送给几个客户,他们的数据库表中存在哪些字段会有所不同EmplTable

EmplTable这些是我的模型类中存在的字段:

    public string EmplId
    {
        get { return this.emplId; }
        set { if (this.emplId != value) { this.emplId = value; RaisePropertyChanged("EmplId"); } }
    }

    public string Name
    {
        get { return this.name; }
        set { if (this.name != value) { this.name = value; RaisePropertyChanged("Name"); } }
    }

    //Bare NAV_Name og ikke Name når axaptaversjonen ikke er 4.1
    public string NAV_Name
    {
        get { return this.nav_Name; }
        set { if (this.nav_Name != value) { this.nav_Name = value; RaisePropertyChanged("NAV_Name"); } }
    }

    public string DataareaId
    {
        get { return this.dataareaId; }
        set { if (this.dataareaId != value) { this.dataareaId = value; RaisePropertyChanged("DataareaId"); } }
    }

    public string NAV_StampId
    {
        get { return this.nav_StampId; }
        set { if (this.nav_StampId != value) { this.nav_StampId = value; RaisePropertyChanged("NAV_StampId"); } }
    }

但是一个客户没有该Nav_Name字段,例如其他客户没有 Name 字段。我当然可以做一些 ifs 和东西,并为不同的EmplTable版本创建不同的模型类,并将当前客户的正确表映射到EmplTable. 但是,如果我可以指定其中一些字段不存在或某些字段不是强制性的,那就太好了。

我没有对不同客户中不存在的字段进行查询EmplTable,但我仍然收到这些“无效的列名”错误。

例如,如果我在执行此查询时EmplTable数据库中不包含,我会得到无效的列名:Nav_name

    public EmplTable GetByIdAndFirma(string id, string firma)
    {
        return this.context.Employees.Where(p => p.EmplId == id && p.DataareaId == firma).FirstOrDefault();
    }

我猜这是因为实体框架的机制什么的,但是有什么设置可以让我指定某些字段不是强制性的吗?

4

0 回答 0