【问题标题】:Map one to one table relation to single entity using entity framework 4.1 Code First approach使用实体框架 4.1 代码优先方法将一对一的表关系映射到单个实体
【发布时间】:2011-06-08 15:37:34
【问题描述】:

我在数据库中的表 Account 和 AccountProperties 之间存在一对一的关系,如下所示。

CREATE TABLE Account(
    [ID] [int] IDENTITY(1,1) NOT NULL,  
    [AccountName] [varchar](255),
    CONSTRAINT [PK_Account]
    PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]

CREATE TABLE AccountProperties(
    [AccountHistoryID] [int] IDENTITY(1,1) NOT NULL,
    [AccountID] [int] NOT NULL,
    [Password] [varchar](50) NULL   
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[AccountProperties]  WITH NOCHECK
ADD  CONSTRAINT [FK_AccountProperties_Account]
FOREIGN KEY([AccountID]) REFERENCES [dbo].[Account] ([ID])

我想使用外键 AccountID 将这两个表映射到单个实体,如下所示。

public class Account
{
    [Key]
    public int ID { get; set; }
    public string AccountName { get; set; }
    public string Password { get; set; }        
}

有人可以使用 fluent API 给我正确的映射吗?

【问题讨论】:

    标签: entity-framework-4.1


    【解决方案1】:

    我在这里找到了解决方案:http://msdn.microsoft.com/en-us/library/bb896233.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 2011-12-17
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多