2015-06-24 24 views
0

我有一個承包商類和一個繼承承包商類的音樂家類。我正在運行遷移,它只會創建一個包含音樂家字段的承建商表。我想要一個承包商表格和音樂家表格,它遵循我的領域模型。它會正確創建Instrument表。這是否與我在類上使用繼承的事實有關?需要兩個獨立的表格,但EF 6只創建一個

public class Contractor 
{ 

    public Guid ID { get; set; } 
    public string FirstName { get; set; }  
    public string LastName { get; set; }  
    public string Email { get; set; }  
    public string ZipCode { get; set; }   
    public string Phone { get; set; }   
    public string Description { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime CreateDate { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime SuspendDate { get; set; } 
    public byte[] ImageData { get; set; } 
    public string ImageMimeType { get; set; } 
    public string ImageName { get; set; } 
    public bool Suspended { get; set; } 

} 

public class Musician : Contractor 
{ 
    public Guid MusiciansId { get; set; } 
    public string WebsiteLink { get; set; }  
    public string YouTubeLink { get; set; }  
    public string SoundCloudLink { get; set; } 
    public string ReverbNationLink { get; set; } 
    public int YearsOfExperience { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime NextDateAvailable { get; set; } 

    public Instrument Instrument { get; set; } 
    public int InstrumentId { get; set; } 

    public Contractor Contractor { get; set; } 
    public Guid ContractorId { get; set; } 


} 

我的遷移腳本:

CreateTable(
      "dbo.Contractor", 
      c => new 
       { 
        ID = c.Guid(nullable: false), 
        FirstName = c.String(), 
        LastName = c.String(), 
        Email = c.String(), 
        ZipCode = c.String(), 
        Phone = c.String(), 
        Description = c.String(), 
        CreateDate = c.DateTime(nullable: false), 
        SuspendDate = c.DateTime(nullable: false), 
        ImageData = c.Binary(), 
        ImageMimeType = c.String(), 
        ImageName = c.String(), 
        Suspended = c.Boolean(nullable: false), 
        UnionMember = c.Boolean(), 
        MusiciansId = c.Guid(), 
        WebsiteLink = c.String(), 
        YouTubeLink = c.String(), 
        SoundCloudLink = c.String(), 
        ReverbNationLink = c.String(), 
        YearsOfExperience = c.Int(), 
        NextDateAvailable = c.DateTime(), 
        InstrumentId = c.Int(), 
        ContractorId = c.Guid(), 
        Discriminator = c.String(nullable: false, maxLength: 128), 
       }) 
      .PrimaryKey(t => t.ID) 
      .ForeignKey("dbo.Contractor", t => t.ContractorId) 
      .ForeignKey("dbo.Instrument", t => t.InstrumentId, cascadeDelete: true) 
      .Index(t => t.InstrumentId) 
      .Index(t => t.ContractorId); 

回答

0

它不是模型類使用繼承一個好主意。 您可以爲承包商添加一個類型值,並創建另一個表爲每種類型的承包商(音樂家爲例):上述音樂家類[表]屬性:

public enum ContractorType 
{ 
    Musician = 0 
} 

public class Contractor 
{ 

    public Guid ID { get; set; } 
    public string FirstName { get; set; }  
    public string LastName { get; set; }  
    public string Email { get; set; }  
    public string ZipCode { get; set; }   
    public string Phone { get; set; }   
    public string Description { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime CreateDate { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime SuspendDate { get; set; } 
    public byte[] ImageData { get; set; } 
    public string ImageMimeType { get; set; } 
    public string ImageName { get; set; } 
    public bool Suspended { get; set; } 

    public ContractorType contractorType { get; set; } 
    public Musician Musician { get; set; } 
} 
+0

感謝您的建議。看起來EF的默認值是TPH(Table per Hierarchy)繼承。這隻創建一個表格。由於表連接的複雜性,TPT(Table per type)的選項不是首選。我從Contoso大學的ASP.net網站獲得了這個教程。 –

相關問題