简体   繁体   中英

Entity Framework 4 Code First Custom table mapping Fluent API issue

First some brief background: I have an existing ASP.NET MVC 1 application using Entity Framework v1 which works fairly well, though because there are getting on to 40 tables the .edmx is getting unwieldy and prone to corruptions with the Visual Studio 2008 designer. What I want to do is to see if it's feasible to migrate the DAL to use EF4 and Code-First.

Initially I'm trying to model simple parent/child relationships but not getting very far. I have 2 tables Client and Address which correspond to the following POCO classes:

public class Client
{
    public int ClientId { get; set; }
    public string Name { get; set; }
    public Address HomeAddress { get; set; }
    public Address WorkAddress { get; set; }
    // More properties here
}

public class Address
{
    public int AddressId { get; set; }
    public string NameOrNumber { get; set; }
    public string Line1 { get; set; }
    // More properties here
}

In addition I have a DbContext class in which I define the relationships using the fluent api:

public class AppContext : DbContext
{
    public SlobContext() : base()
    {
        this.ObjectContext.ContextOptions.LazyLoadingEnabled = true;
    }

    public DbSet<Client> Clients
    {
        get;
        set;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>().MapSingleType().ToTable("Address");
        modelBuilder.Entity<Client>().HasKey(c => c.ClientID);
        modelBuilder.Entity<Client>().HasRequired<Address>(c => c.HomeAddress);
        modelBuilder.Entity<Client>().HasRequired<Address>(c => c.WorkAddress);

        modelBuilder.Entity<Client>()
            .MapSingleType(c => new
            {
                Name = c.Name,
                ClientID = c.ClientID,
                HomeAddressID = c.HomeAddress.AddressID,
                WorkAddressID = c.WorkAddress.AddressID
            })
            .ToTable("Client");
    }
}

So then in my Controller I can return the following as my Model:

Context.Clients.Take(10).OrderBy(i => i.Name)

Which returns 10 results from the database as expected, apart from that it returns a default Address object for Client.WorkAddress and Client.HomeAddress .

My guess is that either I'm setting ObjectContext.ContextOptions.LazyLoadingEnabled = true in the wrong place, or (more likely) I'm not getting my relationships between Client and Address correct.

Your address properties aren't virtual. Lazy loading can't work on a POCO unless the properties are virtual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM