Entity Framework Mapping Question

entity-framework, orm

Solution

It sounds like you have the model working and mapping already?

But you just need to remove the extra navigation property?

If you want to remove the navigation property it is pretty simple (although you can't do it the standard EF designer).

You simply open your EDMX file in an XML editor (easy enough inside VS) and delete the unwanted `<NavigationProperty .../>` from the Customer Entity.

This way the relationship still exists in the Model, but in the class you can only go from Store.TopCustomer

you can't go the other way.

Hope this helps

Alex

Program Manager Entity Framework Team, Microsoft.

Problem

Trying to map the following schema using the Entity Framework. - A Customer can have many associated Stores. - A Store can have many associated Customer - Each Store can have 0 or 1 and only 1 TopCustomer (the criteria to be a TopCustomer is determined in the business logic) this results with the following mapping in VS. Here's the DB Script : ``` 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 ``` Question : How can the TopCustomer association be mapped to a single instance of Customer without creating an extra navigation property on the Customer class ?

Original source