有使用实体框架保存模型c#的问题

时间:2019-02-02 11:41:50

标签: c# entity-framework

保存模型时出现问题。这是我的相关模型。

标记:

public class Mark
    {
        public int Id { get; set; }
        [Display(Name = "Марка")]
        public string MarkName { get; set; }

        public virtual ICollection<CarModel> CarModels { get; set; }
        public virtual ICollection<Car> Cars { get; set; }

        public Mark()
        {
            CarModels = new List<CarModel>();
            Cars = new List<Car>();
        }
    }

CarModel:

public class CarModel
    {
        public int Id { get; set; }
        [Display(Name = "Модель")]
        public string ModelName { get; set; }

        public int MarkId { get; set; }
        public virtual Mark Mark { get; set; }

        public virtual ICollection<Equipment> Equipments { get; set; }
        public virtual ICollection<Car> Cars { get; set; }

        public CarModel()
        {
            Equipments = new List<Equipment>();
            Cars = new List<Car>();
        }
    }

设备:

public class Equipment
    {
        public int Id { get; set; }
        [Required]
        public int Engine { get; set; }
        [Required]
        public int Power { get; set; }
        [Required]
        public int ReleaseYear { get; set; }
        [Required]
        public string DriveType { get; set; }
        [Required]
        public string Transmission { get; set; }
        [Required]
        public string Body { get; set; }
        [Required]
        public int MaxSpeed { get; set; }
        [Required]
        public int Weight { get; set; }
        [Required]
        public int MaxFuelVolume { get; set; }
        [Required]
        public string Color { get; set; }
        [Required]
        public string Picture { get; set; }

        public int CarModelId { get; set; }
        public virtual CarModel CarModel { get; set; }

        public virtual ICollection<Car> Cars { get; set; }

        public Equipment()
        {
            Cars = new List<Car>();
        }
    }

汽车:

public class Car
    {
        public int Id { get; set; }
        [Required]
        public int Price { get; set; }
        [Required]
        public int Count { get; set; }

        public int? MarkId { get; set; }
        public virtual Mark Mark { get; set; }

        public int CarModelId { get; set; }
        public virtual CarModel CarModel { get; set; }

        public int? EquipmentId { get; set; }
        public virtual Equipment Equipment { get; set; }

        public virtual ICollection<Order> Orders { get; set; }

        public Car()
        {
            Orders = new List<Order>();
        }
    }

因此,如果我添加了一辆新车,同时又添加了一个新的品牌,型号和包裹捆绑包,则存储会顺利进行,创建了品牌,将模型与之绑定,将模型与模型捆绑在一起,下面是这辆车的精髓,引用了所有这些内容。

以下是示例代码:

Context db = new Context();

Equipment equip = new Equipment() 
                      { Body = "Седан", Color = "Красный", DriveType = "Передний", Engine = 1600,
                        MaxFuelVolume = 60, MaxSpeed = 250, Picture = "Картинка", Power = 150,
                        ReleaseYear = 2015, Transmission = "Автомат", Weight = 2000 };
Mark mark = new Mark() { MarkName = "BMW" };
CarModel carModel = new CarModel() { ModelName = "M3" };

Car car = new Car();
car.Mark = mark;
car.CarModel = carModel;
car.Equipment = equip;

unitOfWOrk.Cars.Create(car);
unitOfWOrk.Save();

但是,如果我尝试创建汽车并将现有品牌和现有模型链接到该汽车,但是新的全套产品将会失败

  

更新条目时发生错误。有关详细信息,请参见内部异常。

这是示例代码:

Context db = new Context();
Equipment equip = new Equipment() { Body = "Седан", Color = "Красный", DriveType = "Передний", Engine = 1600,
                                    MaxFuelVolume = 60, MaxSpeed = 250, Picture = "Картинка", Power = 150,
                                    ReleaseYear = 2015, Transmission = "Автомат", Weight = 2000 };

Car car = new Car();
car.MarkId = 1;
car.CarModelId = 1;
car.Equipment = equip;

据我了解,它在public int CarModelId {get; set; }模型中的Equipment发誓,因为我尝试做一个int?,但此错误不是。因此,问题在于为什么在创建新数据时,实体会猜测要绑定的内容,而在添加新数据的情况下,这种情况不会发生

InnerException:

  

System.Data.SqlClient.SqlException:INSERT语句与FOREIGN KEY约束“ FK_dbo.Equipments_dbo.CarModels_CarModelId”冲突。在数据库“ D:\ AUTOSTORE \ AUTOSTORE \ APP_DATA \ AUTOSTOREDB.MDF”的表“ dbo.CarModels”的“ Id”列中发生了冲突。该声明已终止。

1 个答案:

答案 0 :(得分:0)

您是否尝试过这些实体连接到您的DbContext? 即使你的数据库有这些实体,与相关变更跟踪的DbContext如果你想必须意识到它们的存在为SaveChanges。 在这种情况下,最简单的方法是手动附加它们:

Mark mark = new Mark() { Id = 1 };
db.Marks.Attach(mark);

CarModel carModel = new CarModel() { Id = 1 };
db.CarModels.Attach(carModel);

接下来,您可以将它们与您的汽车实例相关联:

car.CarModel = carModel;
car.Mark = mark;

这应该行得通,因为就像文档中所说的关于Attach

  

附件用于使用已知的实体重新填充上下文   已经存在于数据库中即可。 SaveChanges因此不会尝试   插入一个附加实体到数据库,因为它假定   已经在那里。请注意,已经存在于   在其他一些国家上下文将有自己的状态设置为不变。   如果实体已经在   状态不变。