0

I have Linq to SQL classes which I have extended with partial classes. Specifically I made a base class which a few classes (SQL tables) inherit.

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

partial public class Blade : DeviceTable { }

Here is the auto-generated ID property in the ORM for class Blade

[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;
        }
    }
}

It allows me to add some polymorphism to some similar tables. However there is a compiler warning

'Blade.ID' hides inherited member 'DeviceTable.ID'. Use the new keywoard if hiding was intended.

I would like to suppress this warning but if I add to the auto-generated file

#pragma warning disable CS0108  

it suppresses the warning until the file is regenerated. Is there a way to add it more permanently?

Adding an example of how the base class is used

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 method

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;
}
djv
  • 15,168
  • 7
  • 48
  • 72
  • I admit I don't know what else you have going on in your code, but from the snippet you've posted, it sounds like you should instead be paying more heed to the warning instead of trying to suppress it. – lc. Mar 07 '18 at 14:36
  • Right. But it is how it is. I will add an example of how the base class is used. – djv Mar 07 '18 at 14:40
  • But hiding property like that is not really a good idea. Are you sure you cannot fix your design instead of supressing warning? – Evk Mar 07 '18 at 14:41
  • I admit I didn't write all of this, but it's currently working. Maybe there is a better way to force ORM generated classes to share a base class. But this is what I have working. I just don't want to clutter up my warnings. – djv Mar 07 '18 at 14:47
  • You can [suppress warnings in your project](https://stackoverflow.com/a/17088432/43846) – stuartd Mar 07 '18 at 14:51
  • Yes but this is designer-generated code and it gets removed when the model is rebuilt. – djv Mar 07 '18 at 14:52
  • @stuartd I could. But I want the warning to cover 99% of the project and it's just this one part that I don't. – djv Mar 07 '18 at 16:47
  • Sounds like you're stuck with the warning then - you don't want to suppress it globally, and you can't permanently suppress it locally.. – stuartd Mar 07 '18 at 17:01

0 Answers0