我有这个模型,它有一个由3个不同的键组成的复合键。当它创建多对多连接表DomainGroupDomainGroup
时,我只获得两个键。一个左,一个右。它引用了字段Key
,它实际上只是可分辨名称的ToLower()
。唯一的问题是可能有许多Domains
或DomainScans
会在主键中产生冲突,因此我们需要DomainscanId
和DomainId
来识别我们哪一行实际上是在谈论。更不用说了,我怀疑它会让我在没有错误的情况下发生碰撞。
CREATE TABLE [dbo].[DomainGroupDomainGroup] (
[DomainGroup_Key] NVARCHAR (128) NOT NULL,
[DomainGroup_Key1] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.DomainGroupDomainGroup] PRIMARY KEY CLUSTERED ([DomainGroup_Key] ASC, [DomainGroup_Key1] ASC),
CONSTRAINT [FK_dbo.DomainGroupDomainGroup_dbo.DomainGroup_DomainGroup_Key] FOREIGN KEY ([DomainGroup_Key]) REFERENCES [dbo].[DomainGroup] ([Key]),
CONSTRAINT [FK_dbo.DomainGroupDomainGroup_dbo.DomainGroup_DomainGroup_Key1] FOREIGN KEY ([DomainGroup_Key1]) REFERENCES [dbo].[DomainGroup] ([Key])
);
以下是模型:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Server.DomainTools.Controller.Domain.Model
{
public class DomainGroup
{
public virtual ICollection<DomainUser> DomainUsers { get; set; }
public virtual ICollection<DomainGroup> DomainGroups { get; set; }
public virtual ICollection<DomainGroup> Parents { get; set; }
public string Name { get; set; }
public string DistinguishedName { get; set; }
[ForeignKey("DomainScan")]
public int DomainScanId { get; set; }
[ForeignKey("Domain")]
public int DomainId { get; set; }
//Composite primary key
//DomainScanId is the version
[Key, Column(Order = 0)]
public string Key { get; set; }
[Key, Column(Order = 1)]
public DomainScan DomainScan { get; set; }
[Key, Column(Order = 2)]
public Domain Domain { get; set; }
}
}