防止WinForms应用中的值重复

时间:2018-11-03 11:02:38

标签: c# winforms linq

我正在尝试将“ Car”对象添加到数据库中。它具有多种值,我不想重复其中的任何一个。示例:

我有一个“ CarType”表。其中有四个字段:

  • 轿车
  • 吉普车
  • 掀背车
  • 双门跑车

添加新车时,用户选择其中一个字段(例如Sedan),但我不希望“ CarType”表中包含另一个“ Sedan”字段。

Application Screenshot

我尝试使用此解决方案防止重复:

        Manufacturer checkManufacturer;
        Model checkModel;
        Levy checkLevy;
        DealType checkDealType;
        FuelType checkFuelType;
        Transmission checkTransmission;
        CarType checkCarType;
        Wheel checkWheel;

        using (var _ctx = new MyAutoGuramEntities2())
        {
            checkManufacturer = _ctx.Manufacturers.Where(c => c.Title.Equals(manufacturerInput)).FirstOrDefault();
            checkModel = _ctx.Models.Where(c => c.Title.Equals(modelInput)).FirstOrDefault();
            checkLevy = _ctx.Levies.Where(c => c.LevyValue.Equals(levyInput)).FirstOrDefault();
            checkDealType = _ctx.DealTypes.Where(c => c.TypeTitle.Equals(dealTypeInput)).FirstOrDefault();
            checkFuelType = _ctx.FuelTypes.Where(c => c.FuelTypeTitle.Equals(fuelTypeInput)).FirstOrDefault();
            checkTransmission = _ctx.Transmissions.Where(c => c.TransmissionType.Equals(transmissionInput)).FirstOrDefault();
            checkCarType = _ctx.CarTypes.Where(c => c.CarTypeValue.Equals(carTypeInput)).FirstOrDefault();
            checkWheel = _ctx.Wheels.Where(c => c.Side.Equals(wheelInput)).FirstOrDefault();
        }



        using (var _context = new MyAutoGuramEntities2())
        {
            //MANUFACTURER
            var manufacturer = new Manufacturer();
            if(checkManufacturer != null)
            {
                if (manufacturerInput != checkManufacturer.Title)
                {
                    manufacturer = new Manufacturer
                    {
                        ID = Guid.NewGuid(),
                        Title = manufacturerInput
                    };
                }
                else
                {
                    manufacturer = new Manufacturer
                    {
                        ID = checkManufacturer.ID,
                        Title = checkManufacturer.Title
                    };
                }
            }
            else
            {
                manufacturer = new Manufacturer
                {
                    ID = Guid.NewGuid(),
                    Title = manufacturerInput
                };
            }


            //MODEL
            var model = new Model();
            if (checkModel != null)
            {
                if (modelInput != checkModel.Title)
                {
                    model = new Model()
                    {
                        ID = Guid.NewGuid(),
                        Title = modelInput,
                        ManufacturerID = manufacturer.ID
                    };
                }
                else
                {
                    model = new Model()
                    {
                        ID = checkModel.ID,
                        Title = checkModel.Title,
                        ManufacturerID = checkModel.ManufacturerID
                    };
                }
            }
            else
            {
                model = new Model()
                {
                    ID = Guid.NewGuid(),
                    Title = modelInput,
                    ManufacturerID = manufacturer.ID
                };
            }


            //CarType
            var carType = new CarType();

            if (checkCarType != null)
            {
                if (carTypeInput != checkCarType.CarTypeValue)
                {
                    carType = new CarType()
                    {
                        ID = Guid.NewGuid(),
                        CarTypeValue = carTypeInput
                    };
                }
                else
                {
                    carType = new CarType()
                    {
                        ID = checkCarType.ID,
                        CarTypeValue = checkCarType.CarTypeValue
                    };
                }

            }
            else
            {
                carType = new CarType()
                {
                    ID = Guid.NewGuid(),
                    CarTypeValue = carTypeInput
                };
            }

每个属性等等。 最后,我将所有内容以这种方式添加到数据库中:

  _context.Manufacturers.Add(manufacturer);
            _context.Models.Add(model);
            _context.CarTypes.Add(carType);
            _context.FuelTypes.Add(fuelType);
            _context.Levies.Add(levy);
            _context.DealTypes.Add(dealType);
            _context.Wheels.Add(wheel);
            _context.Transmissions.Add(transmission);

此代码导致PrimaryKey错误。具体来说:

  

SqlException:违反PRIMARY KEY约束“ PK_CarType”。不能   在对象“ dbo.CarType”中插入重复键。重复键值   是(bb08d23a-f583-4b24-a335-282cd90e9299)。该声明已经   终止。

我了解问题的原因。 CarType的ID不能重复,但是我该如何解决?

0 个答案:

没有答案