0

我有一个非常小的对象图,我正在使用:

public struct Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    etc...
}

public class User
{
    public Address HomeAddress { get; set; }
    public Address WorkAddress { get; set; }
    public string FirstName { get; set; }
    etc...
}

使用 Entity Framework 4.1,我如何将此结构映射到一个表,以便它们映射到如下列:

HomeAddressLine1
HomeAddressLine2
WorkAddressLine1
WorkAddressLine2
FirstName
LastName
etc...
4

1 回答 1

1

EF doesn't support structures. You must use class for your Address and map it as complex type:

public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
}

public class User
{
    public int Id { get; set; }
    public Address HomeAddress { get; set; }
    public Address WorkAddress { get; set; }
    public string FirstName { get; set; }
}


public class Context : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    {
        modelBuilder.Entity<User>()
                    .Property(u => u.HomeAddress.AddressLine1)
                    .ColumnName("HomeAddressLine1");
        // Use the same pattern for all columns of HomeAddress and WorkAddress
    }
}
于 2012-06-13T10:04:51.700 回答