实体框架实体拆分为单个POCO

时间:2015-09-22 15:33:56

标签: entity-framework ef-fluent-api

我正在使用Entity Framework 6.1.3,我想定义一个POCO类(Subscriber),它从三个SQL表中自我保湿。我试图跟踪各种在线参考文献,包括来自Deliveron的参考文献。以下是表格/模型定义......

SQL表

CREATE TABLE [Subscriber]
(
    [SubscriberId] INT NOT NULL PRIMARY KEY,
    [Username] NVARCHAR(50) NOT NULL,
    [Email] NVARCHAR(100) NOT NULL
)
CREATE TABLE [SubscriberDetails]
(
    [SubscriberId] INT NOT NULL PRIMARY KEY,
    [FirstName] NVARCHAR(50) NOT NULL,
    [LastName] NVARCHAR(50) NOT NULL,
    CONSTRAINT [FK_SubscriberDetails_Subscriber]
        FOREIGN KEY ([SubscriberId])
        REFERENCES [Subscriber] ([SubscriberId]) ON DELETE CASCADE
)
CREATE TABLE [SubscriberProfile]
(
    [SubscriberId] INT NOT NULL PRIMARY KEY,
    [Bio] NVARCHAR(MAX) NULL,
    CONSTRAINT [FK_SubscriberProfile_Subscriber]
        FOREIGN KEY ([SubscriberId])
        REFERENCES [Subscriber] ([SubscriberId]) ON DELETE CASCADE
)

域名模型

public class Subscriber {
    public int SubscriberId { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Bio { get; set; }
};

在我的DataContext OnModelCreating()方法中,我添加了以下代码:

modelBuilder.Entity<Subscriber>()
.HasKey(m => m.SubscriberId)
.Map(m => {
    m.ToTable("Subscriber");
    m.Properties(s => new {
        s.SubscriberId,
        s.UserName,
        s.Email
    });
 })
 .Map(m => {
     m.ToTable("SubscriberDetails");
     m.Properties(s => new {
         s.FirstName,
         s.LastName
     });
 })
 .Map(m => {
     m.ToTable("SubscriberProfile");
     m.Properties(s => new {
         s.Bio
     });
 });

虽然这会编译并加载,但当我尝试对DataContext运行查询时,我收到以下错误:

  

System.Data.SqlClient.SqlException:无效的对象名称   'dbo.Subscriber21'

任何人都可以就我的错误给出任何指示吗?

0 个答案:

没有答案