2015-02-05 65 views
0

我有一些手動設置的表和模型,這只是依靠實體的隱式關聯。我沒有edmx文件或設計師或映射向導。手動設置實體框架的表類型繼承

我想通過Table per Type建立一個繼承關聯,這樣我就可以擁有一個表所代表的基類,然後有附加的表示繼承類的附加字段。有沒有一種方法可以用模型和表格做到這一點。默認情況下,實體嘗試按照每個層次結構訪問結構,並引發錯誤,因爲繼承的類成員不在表示基類的表中。

因此,例如:

public class FormBase 
{ 
    [Key] 
    public long Id { get; set; } 

    public int UserId { get; set; } 

    public string FormType { get; set; } 

    public int Status { get; set; } 

    public int? Hidden { get; set; } 

    public DateTime DateSubmitted { get; set; } 

    public DateTime DateFinalized { get; set; } 
} 

CREATE TABLE [dbo].[FormBases] (
    [Id]   BIGINT   IDENTITY (1, 1) NOT NULL, 
    [UserId]  INT   NOT NULL, 
    [FormType]  NVARCHAR (100) NOT NULL, 
    [Status]  INT   DEFAULT ((0)) NOT NULL, 
    [Hidden]  INT   DEFAULT ((0)) NOT NULL, 
    [DateSubmitted] DATETIME2 (7) NULL, 
    [DateFinalized] DATETIME2 (7) NULL, 
    PRIMARY KEY CLUSTERED ([Id] ASC) 
); 

public class CheckRequestModel : FormBase 
{ 
    [Display(Name="Requester Name")] 
    public string RequestorName { get; set; } 
    [Display(Name="Phone Number")] 
    public string PhoneNumber { get; set; } 
    [Display(Name="E-mail")] 
    public string Email { get; set; } 
    [Display(Name="Name")] 
    public string PayToName { get; set; } 
    [Display(Name="Address")] 
    public string PayToAddress { get; set; } 
    [Display(Name="City")] 
    public string PayToCity { get; set; } 
    [Display(Name="State")] 
    public string PayToState { get; set; } 
    [Display(Name="Zip")] 
    public int PayToZip { get; set; } 

    public bool ReceiptAttached { get; set; } 

    [Display(Name="Receipt")] 
    public Attachment Receipt { get; set; } 

    [ForeignKey("Receipt")] 
    public long? ReceiptId { get; set; } 

    public bool InvoiceAttached { get; set; } 
    [Display(Name="Invoice")] 
    public Attachment Invoice { get; set; } 

    [ForeignKey("Invoice")] 
    public long? InvoiceId { get; set; } 

    [Display(Name="Date")] 
    public DateTime? Date { get; set; } 

    [Display(Name="Total Amount")] 
    [DataType(DataType.Currency)] 
    public Decimal? TotalAmount { get; set; } 

    [Display(Name="Purchase Requisition Type")] 
    public string PurchaseRequisitionType { get; set; } 

    [Display(Name="Purpose Description")] 
    public string PurposeDescription { get; set; } 

    [Display(Name="Allocations")] 
    public List<AllocationInformation> Allocations { get; set; } 

    [Display(Name="Allocation Total")] 
    [DataType(DataType.Currency)] 
    public Decimal? AllocationTotal { get; set; } 
} 


CREATE TABLE [dbo].[CheckRequestModels] (
    [Id]      BIGINT   IDENTITY (1, 1) NOT NULL, 
    [UserId]     INT   NOT NULL, 
    [Status]     INT   NOT NULL, 
    [DateSubmitted]   DATETIME2 (7) NULL, 
    [DateFinalized]   DATETIME2 (7) NULL, 
    [FormType]    NVARCHAR (MAX) DEFAULT ('CheckRequest') NULL, 
    [Hidden]     INT   NULL, 
    [RequestorName]   NVARCHAR (MAX) NOT NULL, 
    [PhoneNumber]    NVARCHAR (MAX) NOT NULL, 
    [Email]     NVARCHAR (MAX) NOT NULL, 
    [PayToName]    NVARCHAR (MAX) NOT NULL, 
    [PayToAddress]   NVARCHAR (MAX) NOT NULL, 
    [PayToCity]    NVARCHAR (MAX) NOT NULL, 
    [PayToState]    NVARCHAR (MAX) NOT NULL, 
    [PayToZip]    INT   NOT NULL, 
    [ReceiptAttached]   BIT   DEFAULT ((0)) NOT NULL, 
    [InvoiceAttached]   BIT   DEFAULT ((0)) NOT NULL, 
    [Date]     DATETIME2 (7) NOT NULL, 
    [TotalAmount]    DECIMAL (18) NOT NULL, 
    [PurchaseRequisitionType] NVARCHAR (MAX) NOT NULL, 
    [PurposeDescription]  NVARCHAR (MAX) NOT NULL, 
    [AllocationTotal]   DECIMAL (18) NOT NULL, 
    [InvoiceId]    INT   NULL, 
    [ReceiptId]    INT   NULL, 
    PRIMARY KEY CLUSTERED ([Id] ASC) 
); 

目前我仍然有在繼承表的基類的字段。如果我刪除這些內容,或者是否需要執行其他操作,實體會找出我想要的內容嗎?

+0

我有一個教程http://weblogs.asp.net/ricardoperes/entity-framework-code-first-inheritance,看看它是否有幫助。 – 2015-02-05 18:52:37

+0

好吧,所以我只需要使用'[Table(「Name」)]'註釋來註釋? – Tevis 2015-02-05 19:20:46

+0

好吧我試過了,我認爲它幾乎可以工作,但它一直在尋找一個Discriminator列,就好像它仍在使用層次結構。 – Tevis 2015-02-05 20:09:32

回答

0

指定DbSet爲抽象類型的集合,然後派生類型映射到另一個表:

public class MyDbContext : DbContext 
{ 
    public DbSet<FormBase> Forms { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<CheckRequestModel>().Map(r => { 
      r.ToTable("CheckRequests"); 
     }); 
    } 
} 

這將這樣的伎倆。