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從應用程序類抽象域模型類。