在Entity Framework中播种数据库

时间:2016-06-09 19:17:25

标签: c# entity-framework

当我尝试在我的数据库中填充种子时出现错误:

  

当要求提供Caretaker类型的实体时,发现的实体属于PetsOwner_FF460419E950312585B2229EA7AA6AEAA692190083B7EA5830FB08DEEE79E35D。“

我查看了我发现的所有提示,我检查了类之间的关系,但没有找到任何解决方案。

 context.CareTakers.AddOrUpdate(c => c.Id,
            new Caretaker { Id = 1, Name = "Jan", Surname = "Nowak", Pesel = "29032400352", BankAccountNumber = "05 8202 1016 5582 6188 2074 1719", PhoneNumber = "666-449-292", Accomodations = new List<Accommodation> { context.Accomodations.Find(1) } },
            new Caretaker { Id = 2, Name = "Anna", Surname = "Kowalski", Pesel = "29010209388", BankAccountNumber = "76 1610 1335 0163 2600 0322 4138", PhoneNumber = "789-335-709", Accomodations = new List<Accommodation> { context.Accomodations.Find(2) } },
            new Caretaker { Id = 3, Name = "Sylwia", Surname = "Boski", Pesel = "42022605090", BankAccountNumber = "21 8499 0008 3163 0216 0506 9685", PhoneNumber = "538 -887-645", Accomodations = new List<Accommodation> { context.Accomodations.Find(3) } },
            new Caretaker { Id = 4, Name = "Zbigniew", Surname = "Ryba", Pesel = "10262717572", BankAccountNumber = "61 9614 0008 9550 1997 2431 0213", PhoneNumber = "795-200-495", Accomodations = new List<Accommodation> { context.Accomodations.Find(4) } }
            );

context.Accomodations.AddOrUpdate(a => a.Id,
            new Accommodation { Id = 1, Beginning = new DateTime(2016, 1, 2), End = new DateTime(2016, 1, 3), PricePerDay = 10.5, Pet = context.Pets.Find(1), Caretaker = context.CareTakers.Find(1) },
            new Accommodation { Id = 2, Beginning = new DateTime(2015, 2, 3), End = new DateTime(2016, 2, 4), PricePerDay = 100.0, Pet = context.Pets.Find(2), Caretaker = context.CareTakers.Find(2) },
            new Accommodation { Id = 3, Beginning = new DateTime(2014, 9, 10), End = new DateTime(2016, 9, 11), PricePerDay = 12.3, Pet = context.Pets.Find(3), Caretaker = context.CareTakers.Find(3) },
            new Accommodation { Id = 4, Beginning = new DateTime(2013, 11, 12), End = new DateTime(2016, 12, 13), PricePerDay = 41.4, Pet = context.Pets.Find(4), Caretaker = context.CareTakers.Find(4) }
            );

我的课程如下:

public class Accommodation {
    public int Id { get; set; }//{BAG}
    public virtual Caretaker Caretaker { get; set; }
    public virtual Pet Pet { get; set; }
    public double PricePerDay { get; set; }
    public DateTime Beginning { get; set; }
    public DateTime End { get; set; }

    public double TotalPrice {
        get {
            return (End - Beginning).Days * PricePerDay;
        }
    }
}

[Table("Caretakers")]
public class Caretaker : Person {
    public static int Percent => 20;//C# 6
    public DateTime ObtainedLicense { get; set; }
    public DateTime Hired { get; set; }
    public double Salary => 0;
    public virtual ICollection<Surgery> Surgeries { get; set; }
    public virtual Vet Superior { get; set; }//rekurencyjna 

    //pochodny
    public int AnimalsCurrentlyCareTaken => 0;//Lambda do wyliczenia ilosci opiekowaniymi sie zwierzetyami
    public virtual ICollection<Accommodation> Accomodations { get; set; }
}

0 个答案:

没有答案