Search code examples
entity-frameworkentity-framework-6linq-to-entities

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


I'm having issues when attempting to use the id created on one record for multiple records.

My models look like this:

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; }
}

using the following to seed the database:

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
        }
    }
);

and getting the following error:

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.

Is it possible to use the same "id" (AddressId) that was created from the insertion of the first Person's address in the Address of the second Person? I want to use the same Id for the AddressId of multiple Persons.


Solution

  • If you want to use the same Address entity to multiple Persons, why don't you create the Address first, then use it as many times as you want ?

    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
            }
        }
    );