Updating error using (ling to sql)

前端 未结 1 1030
终归单人心
终归单人心 2021-01-26 07:08

I have two tables, binded by foreign key CarrierID:

Carrier[CarrierID,CarrierName] 
CarrierID = 1, CarrierName = DHL
CarrierID = 2, CarrierName = Fedex
...
Vendo         


        
相关标签:
1条回答
  • 2021-01-26 07:43

    I expect it is this combination:

    // somewhere
    comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim();
    ...
    // somewhere else
    vendor.CarrierID = SelectedCarrierId;
    

    One is using the object-oriented approach, one is using the id-based approach - however, if both are loaded and are incompatible, problems. I wonder if you should use:

    vendor.Carrier = null;
    vendor.CarrierID = SelectedCarrierId;
    

    then there is exactly one definition of which carrier to use.

    Alternatively, handle the carrier separately, for example, instead of:

    comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim();
    

    use:

    var carrier  = context.Carriers.Single(x => x.Id = vendor.CarrierId);
    comVendorCarrier.Text = carrier.CarrierName.Trim();
    

    which then never loads vendor.Carrier as an object.

    0 讨论(0)
提交回复
热议问题