【问题标题】:How to remove duplicate entries in ASP.Net MVC Index View table如何删除 ASP.Net MVC 索引视图表中的重复条目
【发布时间】:2019-08-16 02:22:44
【问题描述】:

我有以下场景:

我有一个Contact,其属性为FirstNameLastName。一个联系人可以有多个PhoneNumbers 和多个EmailAddresses。我已将它们确定为具有共同关系的 3 个实体。请参阅下表定义:

create table Contact
(
    ID int primary key not null identity(1,1),
    FirstName nvarchar(40),
    LastName nvarchar(40)
);
go

create table ContactNumber
(
    ID int primary key not null identity(1,1),
    PhoneNumber nvarchar(40)
);
go

create table EmailAddress
(
    ID int primary key not null identity(1,1),
    EmailAddress nvarchar(40)
);
go

create table UserContact
(
    ID int primary key not null identity(1,1),
    ContactID int foreign key references Contact(ID),
    ContactNumberID int foreign key references ContactNumber(ID),
    EmailID int foreign key references EmailAddress(ID)
);
go

现在我需要在我选择的 Web 应用程序中显示这些用户。我为视图选择了带有 Razor 引擎的 C# MVC。我正在努力在表格中建模这个视图。该表需要操作链接来编辑、删除、创建新联系人和查看联系人的详细信息。我希望结果集如下所示:

FirstName | LastName | ContactNumber | EmailAddress
-------------------------------------------------------
James     | Smith    | 082 111 5454  | james@domain1.com | edit | details | delete
                                     | james@domain2.com
                                     | james@domain3.com
--------------------------------------------------------
Luther    | Arnolds  | 082 111 5455  | luther@domain1.com | edit | details | delete
                     | 082 111 5456  |
--------------------------------------------------------
Sarah     | Jones    | 082 111 5510  |  sarah@domain.com  | edit | details | delete
---------------------------------------------------------
          |          |               |

但是我只是没有找到将ContactNumberIDEmailID 列从UserContact 转换为从它们相应的桥接表映射的写入方法。我现在面临的问题是它正在复制UserContact 中的记录,从而使表包含大量冗余数据。例如,James Smith 的记录将重复 3 次。如何在没有任何重复条目的情况下在 ASP.Net MVC 中实现这一点?

【问题讨论】:

  • 一个电子邮件地址或电话号码可以被多个联系人使用吗?我怀疑不是。我会将 ContactID 作为外键放在 EmailAddress 和 PhoneNumber 表中,然后删除 UserContact 表。你这里没有多对多。您有两个不同的一对多关系。
  • @SeanLange 好的,那么我将如何将关系映射到Contact?因为它拒绝我在 EmailAddressContactNumber 表上添加引用 ContactID 的复合主键。

标签: c# sql-server razor asp.net-mvc-5


【解决方案1】:

我会做这样的事情。请注意,我稍微更改了列名。有一个名为 ID 的列是我的烦恼。无论它在哪个表中,它都是一个 ContactID(或其他)。我也非常不喜欢为相关表中的同一条数据更改列名。

create table Contact
(
    ContactID int primary key not null identity(1,1),
    FirstName nvarchar(40) not null,
    LastName nvarchar(40) not null
);
go

create table ContactNumber
(
    ContactNumberID int primary key not null identity(1,1),
    PhoneNumber nvarchar(40) not null,
    ContactID int not null,
    constraint FK_ContactNumber_Contact_ContactID Foreign Key (ContactID) references Contact(ContactID)
);
go

create table EmailAddress
(
    EmailAddressID int primary key not null identity(1,1),
    EmailAddress nvarchar(40) not null,
    ContactID int not null,
    constraint FK_EmailAddress_Contact_ContactID Foreign Key (ContactID) references Contact(ContactID)
);
go

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2012-04-10
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多