如何抑制ORM设计器生成的代码中的警告

时间:2018-03-07 14:32:05

标签: c# orm pragma

我有Linq to SQL类,我已经扩展了部分类。具体来说,我创建了一个基类,几个类(SQL表)继承。

public abstract class DeviceTable 
{
    public long ID { get; set; } 
}

partial public class Blade : DeviceTable { }

以下是针对类Blade

的ORM中自动生成的ID属性
[Global.System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public long ID {
    get {
        return this._ID;
    }
    set {
        if (((this._ID == value) 
                    == false)) {
            this.OnIDChanging(value);
            this.SendPropertyChanging;
            this._ID = value;
            this.SendPropertyChanged("ID");
            this.OnIDChanged;
        }
    }
}

它允许我在一些类似的表中添加一些多态性。但是有一个编译器警告

  

' Blade.ID'隐藏继承的成员' DeviceTable.ID'。如果打算隐藏,请使用新的keywoard。

我想取消此警告但是如果我添加到自动生成的文件

#pragma warning disable CS0108  

它会在重新生成文件之前禁止警告。有没有办法更永久地添加它?

添加如何使用基类的示例

var tableName = testType.TableDUT;
var ty = Type.GetType(String.Format("CompanyName.Utilities.{0}, Utilities", tableName));
var q = string.Format("select ID, Name from {0} where Name = '{1}'", tableName, fullSerialNumber);
dynamic res;
switch (tableName)
{
    case "Diode":
        res = Support.getDevice((IEnumerable<DeviceTable>)dc.ExecuteQuery<CompanyName.Utilities.Linq.Diode>(q).ToList(), ty);
        if (res.ID == 0) dc.Diodes.InsertOnSubmit((CompanyName.Utilities.Linq.Diode)res);
        break;
    case "Blade":
        res = Support.getDevice((IEnumerable<DeviceTable>)dc.ExecuteQuery<CompanyName.Utilities.Linq.Blade>(q).ToList(), ty);
        if (res.ID == 0) dc.Blades.InsertOnSubmit((CompanyName.Utilities.Linq.Blade)res);
        break;
    case "Engine":
        res = Support.getDevice((IEnumerable<DeviceTable>)dc.ExecuteQuery<CompanyName.Utilities.Linq.Engine>(q).ToList(), ty);
        if (res.ID == 0) dc.Engines.InsertOnSubmit((CompanyName.Utilities.Linq.Engine)res);
        break;
    case "Optic":
        res = Support.getDevice((IEnumerable<DeviceTable>)dc.ExecuteQuery<CompanyName.Utilities.Linq.Optic>(q).ToList(), ty);
        if (res.ID == 0) dc.Optics.InsertOnSubmit((CompanyName.Utilities.Linq.Optic)res);
        break;
    case "Mechanical":
        res = Support.getDevice((IEnumerable<DeviceTable>)dc.ExecuteQuery<CompanyName.Utilities.Linq.Mechanical>(q).ToList(), ty);
        if (res.ID == 0) dc.Mechanicals.InsertOnSubmit((CompanyName.Utilities.Linq.Mechanical)res);
        break;
    default:
        res = null;
        break;
}
if (res.ID == 0)
{
    res.Name = fullSerialNumber;
    res.Rev = rev;
    dc.SubmitChanges();
}

getDevice方法

public static T getDevice<T>(IEnumerable<T> devices, Type type) where T : DeviceTable
{
    if (devices.Count() >= 1)
    {
        return devices.First();
    }
    else if (devices.Count() == 0)
    {
        var instance = Activator.CreateInstance(type);
        return (T)(DeviceTable)instance;
    }
    else return null;
}

0 个答案:

没有答案