种子初始化具有一对多和一对一关系的实体

时间:2013-03-23 05:32:20

标签: c# entity-framework ef-code-first sql-server-express

我正在尝试使用四个表(属性,人员,地址和租约)的种子数据初始化我的SQL数据库。问题是在Person,Address和Lease实体中填充种子数据。当我从Package Management Counsel窗口运行update-database -force时,我收到此错误“更新条目时发生错误。有关详细信息,请参阅内部异常”。 Property表是运行update-database后填充种子数据的四个表中唯一的表。

我假设这个问题是由于一对一和一对多关系使这个数据库设计复杂化,并且会理解为什么我的种子数据无法填充租约的任何帮助,人员和地址实体。

数据库图表:

enter image description here

protected override void Seed(PropertyContext context)
{   
    GetProperty().ForEach(c => context.Properties.Add(c));
    context.SaveChanges();
    GetAddress().ForEach(c => context.Address.Add(c));
    context.SaveChanges();
    GetTenant().ForEach(c => context.Person.Add(c));
    context.SaveChanges();
    GetLease().ForEach(c => context.Leases.Add(c));
    context.SaveChanges();
}

private static List<Property> GetProperty()
{
    var property = new List<Property> {
        new Property
        {
            PropertyId = 1,
            PropName = "Test",
            Address = "13265 Test St.",
            Bathrooms = 3,
            Bedrooms = 2,
            Garage = 2,
            SqFeet= 1400,
            Notes = "Give me a note",
            DateCreated = DateTime.Now,
        }
    };
    return property;
}

private static List<Address> GetAddress() 
{
    var address = new List<Address>{
        new Address
        {
            AddressId = 1,
            City = "Corona",
            State = "CA",
            ZipCode = "92883"
        }
    };
    return address;
}

private static List<Person> GetTenant()
{ 
    var person = new List<Person>{
        new Person
        {
            PersonId = 1,
            FirstName = "Test",
            LastName = "Man",
            EmailAddress = "test@gmail.com",
            Employer = "Verizonwireless",
            JobTitle = "Baseline Tech",
            PropertyOwner = false,
            Notes = "None",
            DateCreated = DateTime.Now,
            LeaseId = 1
         }
    };
    return person;
}

private static List<Lease> GetLease()
{
    var lease = new List<Lease> {
        new Lease
        {
            LeaseId = 1,

            StartDate =DateTime.Today,
            EndDate = DateTime.Now.AddDays(30),
            LeaseAmount = 1550,
            SecurityDeposit = 1550,
            PetDeposit = 0.0,
            OtherDeposit = 0.0,
            Notes = "None",
            LeaseStatus = "Avialable",
            DateCreated = DateTime.Now,
            PropertyId = 1
         }
    };
    return lease;
}


public class Address
{
    [Key]
    [ForeignKey("Property")]
    public int AddressId { get; set; }

    [Required(ErrorMessage = "City name is required.")]
    [MaxLength(35)]
    public string City { get; set; }

    [Required(ErrorMessage = "State name is required.")]
    [Display(Name = "State")]
    [MaxLength(2)]
    public string State { get; set; }

    [Required(ErrorMessage = "Postal code is required.")]
    [Display(Name = "Zip/Postal Code")]
    [MaxLength(5)]
    public string ZipCode { get; set; }

    public virtual Property Property { get; set; }
}

具有一对一关系的模型类:

public class Address
{
    [Key]
    [ForeignKey("Property")]
    public int AddressId { get; set; }

    [Required(ErrorMessage = "City name is required.")]
    [MaxLength(35)]
    public string City { get; set; }

    [Required(ErrorMessage = "State name is required.")]
    [Display(Name = "State")]
    [MaxLength(2)]
    public string State { get; set; }

    [Required(ErrorMessage = "Postal code is required.")]
    [Display(Name = "Zip/Postal Code")]
    [MaxLength(5)]
    public string ZipCode { get; set; }

    public virtual Property Property { get; set; }
}

 public class Property 
{
    public Property() 
    {
        DateCreated = DateTime.Now;
    }

    public int PropertyId { get; set; }

    [Required(ErrorMessage = "Property name is required.")]
    [Display(Name = "Property Name")]
    [MaxLength(75)]
    public string PropName { get; set; }

    [Required(ErrorMessage = "Property address is required.")]
    [MaxLength(200)]
    public string Address { get; set; }

    [Required(ErrorMessage = "Size of the property is required.")]
    [Display(Name = "SQ Feet")]
    public int SqFeet { get; set; }

    [Required(ErrorMessage = "Number of bedrooms is required. Enter zero for none.")]
    public int Bedrooms { get; set; }

    [Required(ErrorMessage = "Number of bathrooms is required.")]
    public int Bathrooms { get; set; }

    public int Garage { get; set; }   // Stores an index value for a dropdown list

    [MaxLength(500, ErrorMessage = "Max number of characters is 500.")]
    public string Notes { get; set; }

    [Required]
    public DateTime DateCreated { get; set; }

}

0 个答案:

没有答案