Entity Framework: 是否可以在运行 seed 方法中将新创建的记录的id插入到另一条记录中

Entity Framework: Is it possible to insert the id of a newly created record into another record while running Seed method

我在尝试将在一条记录上创建的 ID 用于多条记录时遇到问题。

我的模型是这样的:

public class Student 
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int StudentId { get; set; }

    public int PersonId { get; set; }

    public string SID { get; set; }   

    public int AccountId { get; set; }

    public bool Active { get; set; }

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }

    [ForeignKey("AccountId")]
    public virtual Account Account { get; set; }
}

public class Person
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MI { get; set; }
    public int SSN { get; set; }
    public int AddressId { get; set; }
    public int RaceId { get; set; }

    [ForeignKey("AddressId")]
    public Address Addresss { get; set; }  
}

 public class Address
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AddressId { get; set; }
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int ZipCode { get; set; }
}

使用以下内容为数据库播种:

context.Students.AddOrUpdate(x => x.SID,
    new WestCobb.Data.Student
    {
        SID = "99-B50-404324175",
        EnrollmentDate = Convert.ToDateTime("01-30-2017"),
        Active = true,
        Person = new WestCobb.Data.Person
        {
            FirstName = "Brian",
            LastName = "Folks",
            AddressId = 4,
            SSN = 404324175,
            Addresss = new WestCobb.Data.Address // This address!
            {
                StreetAddress = "5076 Lake Charles Court",
                City = "Chicago",
                State = "Illinois",
                ZipCode = 60208
            }
        },
        Account = new WestCobb.Data.Account
        {
            AccountNumber = "M00000000000088",
            AccountTypeId = 1,
            Priority = 1,
            Rate = 225.00m,
            BillingCycleId = 1,
            CreateDate = Convert.ToDateTime("01-30-2017"),
            Active = true
        }
    },
    // Manually set AddressId of this Person to AddressId of previous Person added 
    new WestCobb.Data.Student
    {
        SID = "99-A50-404527896",
        EnrollmentDate = Convert.ToDateTime("01-30-2017"),
        Active = true,
        Person = new WestCobb.Data.Person
        {
            FirstName = "Arthur",
            LastName = "Clue",
            SSN = 404527896,
            AddressId = context.Addresses.Last().AddressId // This seems not to work
        },
        Account = new WestCobb.Data.Account
        {
            AccountNumber = "W89322198433588",
            AccountTypeId = 1,
            Priority = 2,
            Rate = 225.00m,
            BillingCycleId = 1,
            CreateDate = Convert.ToDateTime("01-30-2017"),
            Active = true
        }
    }
);

并出现以下错误:

LINQ to Entities does not recognize the method 'WestCobb.Data.Address Last[Address](System.Linq.IQueryable`1[WestCobb.Data.Address])' method, and this method cannot be translated into a store expression.

是否可以使用通过将第一个人的地址插入第二个人的地址而创建的相同 "id" (AddressId)?我想对多个人的 AddressId 使用相同的 Id。

如果您想对多个人使用同一个地址实体,为什么不先创建地址,然后根据需要多次使用它?

var myAwesomeAddress = new WestCobb.Data.Address()
{
    StreetAddress = "5076 Lake Charles Court",
    City = "Chicago",
    State = "Illinois",
    ZipCode = 60208
};

// You should have a DbSet of your Addresses right ?
context.Addresses.AddOrUpdate(x => x.Id, // Use any discriminator
    myAwesomeAddress
);

context.Students.AddOrUpdate(x => x.SID,
    new WestCobb.Data.Student
    {
        SID = "99-B50-404324175",
        EnrollmentDate = Convert.ToDateTime("01-30-2017"),
        Active = true,
        Person = new WestCobb.Data.Person
        {
            FirstName = "Brian",
            LastName = "Folks",
            AddressId = 4,
            SSN = 404324175,
            Addresss = myAwesomeAddress
        }
    },
    new WestCobb.Data.Student
    {
        SID = "99-A50-404527896",
        EnrollmentDate = Convert.ToDateTime("01-30-2017"),
        Active = true,
        Person = new WestCobb.Data.Person
        {
            FirstName = "Arthur",
            LastName = "Clue",
            SSN = 404527896,
            Address = myAwesomeAddress // here
        }
    }
);