简体   繁体   English

实体框架4代码优先自定义表映射Fluent API问题

[英]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. 首先是一些简短的背景:我有一个使用Entity Framework v1的现有ASP.NET MVC 1应用程序,它运行得相当好,但是因为有了40个表,所以.edmx变得笨拙并且容易受到Visual Studio 2008设计器的破坏。 What I want to do is to see if it's feasible to migrate the DAL to use EF4 and Code-First. 我想要做的是看看将DAL迁移到使用EF4和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: 我有2个表ClientAddress ,对应于以下POCO类:

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: 另外我有一个DbContext类,我在其中使用流畅的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 . 除了返回Client.WorkAddressClient.HomeAddress的默认Address对象之外,它会按预期返回数据库中的10个结果。

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. 我的猜测是,我要么在错误的地方设置ObjectContext.ContextOptions.LazyLoadingEnabled = true ,或者(更有可能)我没有让ClientAddress之间的关系正确。

Your address properties aren't virtual. 您的地址属性不是虚拟的。 Lazy loading can't work on a POCO unless the properties are virtual. 除非属性是虚拟的,否则延迟加载不能在POCO上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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