Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架关系问题_Entity Framework_Entity Framework 5 - Fatal编程技术网

Entity framework 实体框架关系问题

Entity framework 实体框架关系问题,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,我正在努力跟踪当地总部和州总部之间是否记录了汽车部件的维修情况。我在零件表中得到一个名为Car_Vin的附加字段。此外,当我抓取与汽车相关的记录时,我的记录收集总是以空的形式返回。有人能解释为什么会发生这两个问题吗 public Car { [Key] public string VIN{get;set;} //More fields go here public virtual ICollection<StateHQRecord> StateHQRe

我正在努力跟踪当地总部和州总部之间是否记录了汽车部件的维修情况。我在零件表中得到一个名为Car_Vin的附加字段。此外,当我抓取与汽车相关的记录时,我的记录收集总是以空的形式返回。有人能解释为什么会发生这两个问题吗

public Car
{
    [Key]
    public string VIN{get;set;}
    //More fields go here
    public virtual ICollection<StateHQRecord> StateHQRecords{get;set;}
    public virtual ICollection<LocalHQRecord> LocalHQRecords{get;set;}
}

public Part{
{
    [Key]
    public string PartNumber{get;set;}
    //More fields go here
    public virtual ICollection<StateHQRecord> StateHQRecords{get;set;}
    public virtual ICollection<LocalHQRecord> LocalHQRecords{get;set;}
}

public class StateHQRecord
{
    public string VIN{get;set;}
    public string PartNumber{get;set;}
    public Car Car{get;set;}
    public Part Part{get;set;}
    public bool isRepaired{get;set;}
}

public class LocalHQRecord
{
    public string VIN{get;set;}
    public string PartNumber{get;set;}
    public Car Car{get;set;}
    public Part Part{get;set;}
    public bool isRepaired{get;set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<StateHQRecord>().HasKey(te => new {te.VIN, te.PartNumber});
        modelBuilder.Entity<StateHQRecord>().Map(m => m.ToTable("StateHQRecords"));
        modelBuilder.Entity<LocalHQRecord>().HasKey(te => new {te.VIN, te.PartNumber});
        modelBuilder.Entity<LocalHQRecord>().Map(m => m.ToTable("LocalHQRecords")); 

    modelBuilder.Entity<Car>().HasKey(a => a.VIN);
        modelBuilder.Entity<Car>().
                HasMany(a => a.StateHQRecords).
                WithRequired(te => te.Car).
                HasForeignKey(te => te.VIN);
    modelBuilder.Entity<Car>().
                HasMany(a => a.LocalHQRecords).
                WithRequired(te => te.Car).
                HasForeignKey(te => te.VIN);

    modelBuilder.Entity<Part>().HasKey(a => a.PartNumber);
        modelBuilder.Entity<Part>().
                HasMany(a => a.StateHQRecords).
                WithRequired(te => te.Part).
                HasForeignKey(te => te.PartNumber);
    modelBuilder.Entity<Part>().
                HasMany(a => a.LocalHQRecords).
                WithRequired(te => te.Part).
                HasForeignKey(te => te.PartNumber);

}


public void AddCarToLocalHQ(Car car,Part part)
{
    var currentCarEntityState=EntityState.Modified;
    var currentPartEntityState=EntityState.Modified;
    var currentCar=context.Cars.FirstOrDefault(c=>c.VIN==car.VIN);
    var currentPart=context.Parts.FirstOrDefault(p=>p.PartNumber==part.PartNumber);

    if(currentCar==null)
    {
        currentCar=car;
        currentCarEntityState=EntityState.Added;
    }
    if(currentPart==null)
    {
        currentPart=part;
        currentPartEntityState=EntityState.Added;
    }

    var currentRecord=context.LocalHQRecords.FirstOrDefault(r=>r.VIN==currentCar.VIN && r.PartNumber==currentPart.PartNumber);
    if(currentRecord!=null)
    {
        throw new DuplicateRecordException();
    }

    currentRecord=new LocalHQRecord{Car=currentCar,Part=currentPart};
    currentCar.LocalHQRecords.Add(currentRecord);
    currentPart.LocalHQRecords.Add(currentRecord);
    context.Entry(currentCar).State=currentCarEntityState;
    context.Entry(currentPart).State=currentPartEntityState;
    context.SaveChanges();
}