【问题标题】:Bind Complex Objects Within Model To Database将模型中的复杂对象绑定到数据库
【发布时间】:2012-11-22 14:53:31
【问题描述】:

我今天下午看了很多文章,但我似乎找不到一篇完整的文章来处理模型中的复杂对象来解决我正在尝试做的事情。

我正在使用 Code First 方法,但我的模型没有直接改变我的数据库结构。因此,如果数据库结构发生变化,我会手动更改模型。

话虽如此,我有下表:

Name
Address1
Address2
Address3
City
State
Zip
Latitude
Longitude

我想构建一个如下所示的模型:

public class Person
{
  public string Name { get; set; }
  public Address Address { get; set; }
  public Coordinates Coordinates { get; set; }
}
public class Address {
  public string Address1 { get; set; }
  public string Address2 { get; set; }
  public string Address3 { get; set; }
  public string City { get; set; }
  public string State { get; set; }
  public string Zip { get; set; }
}
public class Coordinates
{
  public float Latitude { get; set; }
  public float Longitude { get; set; }
}

在我看来,这似乎是一种整洁的、面向对象的方法,可以处理从数据库返回的一对一数据。代替所有这些单独的变量,相似的数据被分组到子类中。

我的问题是:如何使用 EF 将这个复杂模型绑定到我的数据库?如果这不可能,我该如何编写自定义活页夹来以这种方式处理数据。

还是我想错了。是否有不同的方法来解决这个问题,或者数据的结构是否应该不同?

编辑

根据下面 Jason Meckley 的回复,这是我用来完成此任务的最终代码。

namespace MyNamespace
{
  public partial class Person
  {
    public string Name { get; set; }
    public Address Address { get; set; }
    public Coordinates Coordinates { get; set; }
  }
  [ComplexType]
  public class Address
  {
    [Display(Name = "Address")]
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
  }
  [ComplexType]
  public class Coordinates
  {
    public float Latitude { get; set; }
    public float Longitude { get; set; }
  }

  public partial class DATABASE_CONNECTION_NAME : DbContext
  {
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Address.Address1)
          .HasColumnName("Address1");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Address.Address2)
          .HasColumnName("Address2");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Address.Address3)
          .HasColumnName("Address3");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Address.City)
          .HasColumnName("City");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Address.State)
          .HasColumnName("State");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Address.Zip)
          .HasColumnName("Zip");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Coordinates.Latitude)
          .HasColumnName("Latitude");
      modelBuilder
          .Entity<Person>()
          .Property(x => x.Coordinates.Longitude)
          .HasColumnName("Longitude");
    }
  }
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 model-view-controller model


    【解决方案1】:

    使用属性[ComplexType] 装饰AddressCoordinates。在您的 dbcontext 子类中覆盖 OnModelCreating 并将复杂属性映射到适当的列。

    默认情况下,复杂对象将像 Address_AddressCoordinates_Latitude 这样映射,因此您需要使用类似的东西覆盖默认映射

    modelBuilder
              .Entity<Person>()
              .Property(x => x.Address.Address1)
              .HasColumnName("Address1");
    

    【讨论】:

    • 感谢您的简洁。完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2013-02-04
    • 2016-07-06
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    相关资源
    最近更新 更多