实体框架映射问题
Posted
技术标签:
【中文标题】实体框架映射问题【英文标题】:Entity Framework Mapping Question 【发布时间】:2009-06-20 14:04:24 【问题描述】:尝试使用实体框架映射以下架构。
一个客户可以有许多关联的商店。 一个商店可以有许多关联的客户 每个 Store 可以有 0 个或 1 个且只有 1 个 TopCustomer(成为 TopCustomer 的条件在业务逻辑中确定)这会在 VS 中产生以下映射。
这是数据库脚本:
USE [TestDb]
GO
/****** Object: Table [dbo].[Customer] Script Date: 06/20/2009 09:53:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customer](
[CustomerId] [uniqueidentifier] NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Store] Script Date: 06/20/2009 09:53:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Store](
[StoreId] [uniqueidentifier] NOT NULL,
[StoreName] [nvarchar](50) NOT NULL,
[TopCustomer] [uniqueidentifier] NULL,
CONSTRAINT [PK_Store] PRIMARY KEY CLUSTERED
(
[StoreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[CustomerStore] Script Date: 06/20/2009 09:53:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CustomerStore](
[CustomerId] [uniqueidentifier] NOT NULL,
[StoreId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_CustomerStore] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC,
[StoreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: ForeignKey [FK_CustomerStore_Customer] Script Date: 06/20/2009 09:53:52 ******/
ALTER TABLE [dbo].[CustomerStore] WITH CHECK ADD CONSTRAINT [FK_CustomerStore_Customer] FOREIGN KEY([CustomerId])
REFERENCES [dbo].[Customer] ([CustomerId])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CustomerStore] CHECK CONSTRAINT [FK_CustomerStore_Customer]
GO
/****** Object: ForeignKey [FK_CustomerStore_Store] Script Date: 06/20/2009 09:53:52 ******/
ALTER TABLE [dbo].[CustomerStore] WITH CHECK ADD CONSTRAINT [FK_CustomerStore_Store] FOREIGN KEY([StoreId])
REFERENCES [dbo].[Store] ([StoreId])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CustomerStore] CHECK CONSTRAINT [FK_CustomerStore_Store]
GO
/****** Object: ForeignKey [FK_Store_TopCustomer] Script Date: 06/20/2009 09:53:52 ******/
ALTER TABLE [dbo].[Store] WITH CHECK ADD CONSTRAINT [FK_Store_TopCustomer] FOREIGN KEY([TopCustomer])
REFERENCES [dbo].[Customer] ([CustomerId])
GO
ALTER TABLE [dbo].[Store] CHECK CONSTRAINT [FK_Store_TopCustomer]
GO
问题:
如何在不为 Customer 类创建额外导航属性的情况下将 TopCustomer 关联映射到 Customer 的单个实例?
【问题讨论】:
抱歉,您使用哪个应用程序进行映射? 【参考方案1】:听起来你已经有模型工作和映射了?
但您只需要删除额外的导航属性?
如果你想删除导航属性,这很简单(虽然你不能用标准的 EF 设计器来做)。
您只需在 XML 编辑器中打开您的 EDMX 文件(在 VS 中很简单),然后从客户实体中删除不需要的 <NavigationProperty .../>
。
这样关系在Model中仍然存在,但在类中你只能从 Store.TopCustomer
你不能走另一条路。
希望对你有帮助
亚历克斯
项目经理实体框架团队,Microsoft。
【讨论】:
以上是关于实体框架映射问题的主要内容,如果未能解决你的问题,请参考以下文章