EF4 - POCO 可以同时用作实体和复杂类型吗?

发布于 2024-10-14 22:01:01 字数 1634 浏览 2 评论 0原文

我正在使用 EF4 CTP5。这是我的 POCO:

public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Address> Addresses { get; set; }
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public decimal Total { get; set; }
    public Address ShippingAddress { get; set; }
    public Address BillingAddress { get; set; }
}

有没有办法让 Address 成为 Order 类的 ComplexType?在玩过这个之后,我猜不是,但也许有一种我没有见过的方法。

编辑:为了回应下面的Shawn,我尽了最大努力:

//modelBuilder.Entity<Order>().Ignore(o => o.BillingAddress);
//modelBuilder.Entity<Order>().Ignore(o => o.ShippingAddress);
modelBuilder.Entity<Order>()
    .Property(o => o.BillingAddress.City).HasColumnName("BillingCity");

运行时失败,错误“配置的属性“BillingAddress”不是实体“Order”上声明的属性'." 尝试使用 Ignore() 不起作用。接下来,Hanselman 文章是 CTP4,但 CTP5 等效项是:

modelBuilder.Entity<Order>().Map(mapconfig =>
{
    mapconfig.Properties(o => new {
        o.Id
        , o.Total
        , o.BillingAddress.City
    });
    mapconfig.ToTable("Orders");
});

失败并出现错误“类型为“Order”的属性“BillingAddress.City”无法包含在其映射中。”

我放弃了。也许最终版本会有这样的东西。或者也许我需要切换到 NHibernate =)

I am using EF4 CTP5. Here are my POCOs:

public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Address> Addresses { get; set; }
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public decimal Total { get; set; }
    public Address ShippingAddress { get; set; }
    public Address BillingAddress { get; set; }
}

Is there a way to get Address to be a ComplexType for the Order class? After playing around with this, I'm guessing not, but maybe there's a way I haven't seen.

EDIT: In response to Shawn below, I gave it my best shot:

//modelBuilder.Entity<Order>().Ignore(o => o.BillingAddress);
//modelBuilder.Entity<Order>().Ignore(o => o.ShippingAddress);
modelBuilder.Entity<Order>()
    .Property(o => o.BillingAddress.City).HasColumnName("BillingCity");

Fails at runtime with error "The configured property 'BillingAddress' is not a declared property on the entity 'Order'." Trying to use Ignore() doesn't work. Next, the Hanselman article is CTP4, but the CTP5 equivalent is:

modelBuilder.Entity<Order>().Map(mapconfig =>
{
    mapconfig.Properties(o => new {
        o.Id
        , o.Total
        , o.BillingAddress.City
    });
    mapconfig.ToTable("Orders");
});

Fails with error "Property 'BillingAddress.City' of type 'Order' cannot be included in its mapping."

I give up. Maybe the final release will have something like this. Or maybe I need to switch to NHibernate =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-10-21 22:01:01

您需要做的就是将ComplexTypeAttribute放在Address类上:

[ComplexType]
public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

或者,您可以通过流畅的API来实现这一点:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ComplexType<Address>();
}

但是您不能将Address类型既是实体又是一种复杂类型,这是这样或那样的。

看一下这篇博文,我在其中详细讨论了这个问题:

EF Code First CTP5 中的关联:第 1 部分 – 复杂类型

All you need to do is to place ComplexTypeAttribute on Address class:

[ComplexType]
public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

Alternatively, you can achieve this by fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ComplexType<Address>();
}

But you cannot have Address type as to be both an Entity and a Complex Type, it's one way or another.

Take a look at this blog post where I discuss this at length:

Associations in EF Code First CTP5: Part 1 – Complex Types

橘亓 2024-10-21 22:01:01

如果您希望 Address 与 Order 位于同一个表中,则必须在 DbContext OnModelCreating 重写中告诉 EF。

看一下这里: ​​http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx

If you want Address to be in the same table as Order, you're going to have to tell EF that in the DbContext OnModelCreating override.

Take a look here: http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文