2

让我EF6项目说[数据库,第一个方法]我有一个名为Address复杂类型[我只想澄清我的复杂类型没有任何身份,仅仅是一个合并独立数据的汇总,它甚至不负责其自身的持续性]复杂型内DDD项目的数据库,第一个模型

目前我有所有与该地址相关的字段作为地址的组成部分的直接属性,并具有以下自动生成的定义等级:

public class Person 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public Nullable<int> Easting { get; set; } 
    public Nullable<int> Northing { get; set; } 
    public string Building { get; set; } 
    public string County { get; set; } 
    public string PostCode { get; set; } 
    public string StreetName { get; set; } 
    public string StreetNumber { get; set; } 
    public string Town { get; set; } 
    public string Unit { get; set; } 
    public string Village { get; set; } 
    public int CountryId { get; set; } 
} 

理想情况下,我想有东西喜欢E以下[我每次更新的数据库模型]:

public class Person   
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public Address Address { get; set; } 
} 

public class Address 
{ 
    public Nullable<int> Easting { get; set; } 
    public Nullable<int> Northing { get; set; } 
    public string Building { get; set; } 
    public string County { get; set; } 
    public string PostCode { get; set; } 
    public string StreetName { get; set; } 
    public string StreetNumber { get; set; } 
    public string Town { get; set; } 
    public string Unit { get; set; } 
    public string Village { get; set; } 
    public int CountryId { get; set; } 
} 

我将如何能够拥有所有地址相关字段作为被叫地址聚集现场,当我从数据库(SQL Server 2012)更新我的模型?

据我所知,唯一的出路是修改T4模板。如果您唯一建议的解决方案是T4模板替换,请您向我展示一些采取类似策略的示例项目或提供您自己的版本。

+0

你使用代码优先方法?使用EF6和代码优先方法时,我对复杂类型没有任何问题。前缀为“Address”的列(例如'Address_Easting','Address_Northing','Address_Building')应默认在表“Person”中创建。如果您使用数据库优先方法,那么只需按照地址相关列的命名模式。 – 2014-10-18 21:58:15

+2

我特别提到这是问题标题中的数据库优先模型。 – MHOOS 2014-10-20 08:02:21

+0

您是否尝试将地址属性移动到EF设计器中的复杂类型(http://msdn.microsoft.com/zh-cn/data/jj680147.aspx)? – Chris 2014-10-22 15:51:47

回答

0

当您在EF使用数据库第一种方法你打下发电机产生的类的所有责任。所以你不能在这种方法中获得复杂类型的地址。你应该使用其他方法来获得你想要的。如果我是你,我会使用代码优先的方法,并写入从现有数据库到代码中的类的映射。

0

您可以使用TPH达到你想要什么,例如:在你的类

您将有以下几点:

1类Person

public class Person 
{ 
    public int Id{get; set;} 
    public string Name{get; set;} 
} 

2-类地址从继承班级人员

public class Address: Person 
{ 
    public int? Easting { get; set; } 
    public int? Northing { get; set; } 
    public string Building { get; set; } 
    public string County { get; set; } 
    public string PostCode { get; set; } 
    public string StreetName { get; set; } 
    public string StreetNumber { get; set; } 
    public string Town { get; set; } 
    public string Unit { get; set; } 
    public string Village { get; set; } 
    public int CountryId { get; set; } 
} 

并在您的DbContext类中调用了“Entities”f或例子,你只定义如下

public class Entities: DbContext 
{ 
    public DbSet<Person> People{get; set;} 
} 

所以这会在你的数据库中产生什么?

1-它会生成一个表包含来自人和地址类两种特性被称为人

2 - 你可以访问数据的人无论是从个人还是从地址这样

var db=new Entities(); 
var person= db.People.OfType<Person>(); // this will give you only Id and Name properties 
var address= db.People.OfType<Address>(); // this will give you the person and address details together 

希望这将帮助您

0

您可以使用DTOsAutomapper从应用程序类抽象域模型类。