Question about the wireframe of objects

Trying to map the following schema using Entity Framework.

  • A customer may have many related stores.
  • A store can have many related customers.
  • Each store can have 0 or 1 and only 1 TopCustomer (the criteria that TopCustomer should be defined in the business logic)

alt text

this leads to the next mapping in VS.

alt text


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 one instance of the Client without creating an additional navigation property in the Customer class?

+3
source share
1 answer

It looks like you already have a model and mapping?

But you just need to remove the additional navigation property?

, ( EF).

EDMX XML ( VS) <NavigationProperty .../> Customer.

, , Store.TopCustomer

.

,

Entity Framework Team, Microsoft.

+4

Source: https://habr.com/ru/post/1710912/


All Articles