在数据库设计中,一种常见的做法是在“一个”(或子)侧用外键表示一对多关系。实体框架很好地处理了这种情况。

但是,我遇到的情况是通过联接表表示一对多关系,其中表上的两个外键之一具有唯一约束。

如何配置实体框架以利用此联接表?

在我当前的状态下,当对一个/子实体进行简单的读取查询时,Entity Frameworks会抛出一个异常(正如预期的那样),即一个/子表缺少基于导航的常规名称的列属性。

最佳答案

如果我有以下DDL。

员工(M:N)到ParkingArea。但是,约束在链接表中仅保留一个Employee,因此为1:N。

-启动TSQL

Use OrganizationReverseDB
GO


SET NOCOUNT ON


IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[EmployeeParkingAreaLink]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN DROP TABLE [dbo].[EmployeeParkingAreaLink]
END
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[ParkingArea]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [dbo].[ParkingArea]
END
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [dbo].[Employee]
END


IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN DROP TABLE [dbo].[Employee]
END


IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Department]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN DROP TABLE [dbo].[Department]
END

GO


CREATE TABLE [dbo].[Department](
    [DepartmentUUID] [uniqueidentifier] NOT NULL,
    [TheVersionProperty] [timestamp] NOT NULL,
    [DepartmentName] [nvarchar](80) NULL,
    [CreateDate] [datetime] NOT NULL,
    )


ALTER TABLE dbo.[Department] ADD CONSTRAINT PK_Department PRIMARY KEY NONCLUSTERED ([DepartmentUUID])
GO

ALTER TABLE [dbo].[Department] ADD CONSTRAINT CK_DepartmentName_Unique UNIQUE ([DepartmentName])
GO


CREATE TABLE [dbo].[Employee] (

    [EmployeeUUID] [uniqueidentifier] NOT NULL,
    [ParentDepartmentUUID] [uniqueidentifier] NOT NULL,
    [TheVersionProperty] [timestamp] NOT NULL,
    [SSN] [nvarchar](11) NOT NULL,
    [LastName] [varchar](64) NOT NULL,
    [FirstName] [varchar](64) NOT NULL,
    [CreateDate] [datetime] NOT NULL,
    [HireDate] [datetime] NOT NULL
    )

GO

ALTER TABLE dbo.Employee ADD CONSTRAINT PK_Employee PRIMARY KEY NONCLUSTERED (EmployeeUUID)
GO


ALTER TABLE [dbo].[Employee] ADD CONSTRAINT CK_SSN_Unique UNIQUE (SSN)
GO

ALTER TABLE [dbo].[Employee] ADD CONSTRAINT FK_EmployeeToDepartment FOREIGN KEY (ParentDepartmentUUID) REFERENCES dbo.Department (DepartmentUUID)
GO





IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[ParkingArea]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN DROP TABLE [dbo].[ParkingArea]
END
GO

CREATE TABLE [dbo].[ParkingArea]
(
ParkingAreaUUID [UNIQUEIDENTIFIER] NOT NULL DEFAULT NEWSEQUENTIALID()
, ParkingAreaName varchar(24) not null
, CreateDate smalldatetime not null
)

GO

ALTER TABLE dbo.ParkingArea ADD CONSTRAINT PK_ParkingArea PRIMARY KEY NONCLUSTERED (ParkingAreaUUID)
GO

ALTER TABLE [dbo].[ParkingArea] ADD CONSTRAINT CK_ParkingAreaName_Unique UNIQUE (ParkingAreaName)
GO



IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[EmployeeParkingAreaLink]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN DROP TABLE [dbo].[EmployeeParkingAreaLink]
END
GO

CREATE TABLE [dbo].[EmployeeParkingAreaLink] (
    /* [LinkSurrogateUUID] [uniqueidentifier] NOT NULL, */
    [TheEmployeeUUID] [uniqueidentifier] NOT NULL,
    [TheParkingAreaUUID] [uniqueidentifier] NOT NULL
)


GO

/*
ALTER TABLE dbo.EmployeeParkingAreaLink ADD CONSTRAINT PK_EmployeeParkingAreaLink PRIMARY KEY NONCLUSTERED (LinkSurrogateUUID)
*/
GO

ALTER TABLE [dbo].[EmployeeParkingAreaLink] ADD CONSTRAINT FK_EmployeeParkingAreaLinkToEmployee FOREIGN KEY (TheEmployeeUUID) REFERENCES dbo.Employee (EmployeeUUID)
GO

ALTER TABLE [dbo].[EmployeeParkingAreaLink] ADD CONSTRAINT FK_EmployeeParkingAreaLinkToParkingArea FOREIGN KEY (TheParkingAreaUUID) REFERENCES dbo.ParkingArea (ParkingAreaUUID)
GO

ALTER TABLE [dbo].[EmployeeParkingAreaLink] ADD CONSTRAINT CONST_UNIQUE_EmpUUID UNIQUE (TheEmployeeUUID )
GO

ALTER TABLE [dbo].[EmployeeParkingAreaLink] ADD CONSTRAINT CONST_UNIQUE_EmpUUID_PAUUID UNIQUE (TheEmployeeUUID , TheParkingAreaUUID)
GO


Insert into dbo.Department ( [DepartmentUUID], [DepartmentName] , CreateDate )
    select '10000000-0000-0000-0000-000000000001' , 'DepartmentOne' , CURRENT_TIMESTAMP
    union all   select '10000000-0000-0000-0000-000000000002' , 'DepartmentTwo' , CURRENT_TIMESTAMP

Insert into dbo.Employee ( EmployeeUUID, SSN , CreateDate, HireDate , LastName, FirstName , ParentDepartmentUUID)
    select '20000000-0000-0000-0000-000000000001' , '111-11-1111' , CURRENT_TIMESTAMP , '01/31/2001' , 'Smith' , 'John' , '10000000-0000-0000-0000-000000000001'
    union all   select '20000000-0000-0000-0000-000000000002' , '222-22-2222' , CURRENT_TIMESTAMP, '02/28/2002' , 'Jones' , 'Mary' , '10000000-0000-0000-0000-000000000002'

Insert into dbo.ParkingArea ( [ParkingAreaUUID], [ParkingAreaName] , CreateDate )
    select '30000000-0000-0000-0000-000000000001' , 'ParkingAreaOne' , CURRENT_TIMESTAMP
    union all   select '30000000-0000-0000-0000-000000000002' , 'ParkingAreaTwo' , CURRENT_TIMESTAMP


INSERT INTO [dbo].[EmployeeParkingAreaLink] (   [TheEmployeeUUID] , [TheParkingAreaUUID] )
        Select '20000000-0000-0000-0000-000000000001' , '30000000-0000-0000-0000-000000000001'
union all       Select '20000000-0000-0000-0000-000000000002' , '30000000-0000-0000-0000-000000000002'


约束“ CONST_UNIQUE_EmpUUID”是您正在说的设置。

EmployeeEntity像这样:

[Serializable]
public partial class EmployeeEFEntity
{

public EmployeeEFEntity()
{
    CommonConstructor();
}
private void CommonConstructor()
{
    //this.MyParkingAreas = new List<ParkingAreaEFEntity>();
}



//EF Tweaks
public virtual Guid? ParentDepartmentUUID { get; set; }

public virtual Guid? EmployeeUUID { get; set; }

public virtual byte[] TheVersionProperty { get; set; }

public virtual DepartmentEFEntity ParentDepartment { get; set; }

public virtual string SSN { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual DateTime HireDate { get; set; }

public virtual ICollection<ParkingAreaEFEntity> MyParkingAreas { get; set; }

public ParkingAreaEFEntity MyOneParkingAreaEFEntity {

    get
    {
        return MyParkingAreas.FirstOrDefault();
    }
    set
    {
        /* check for more than one here */
        this.AddParkingArea(pa);
    }
}


public virtual void AddParkingArea(ParkingAreaEFEntity pa)
{
    if (!pa.MyEmployees.Contains(this))
    {
        pa.MyEmployees.Add(this);
    }
    if (!this.MyParkingAreas.Contains(pa))
    {
        this.MyParkingAreas.Add(pa);
    }
}


public virtual void RemoveParkingArea(ParkingAreaEFEntity pa)
{
    if (pa.MyEmployees.Contains(this))
    {
        pa.MyEmployees.Remove(this);
    }
    if (this.MyParkingAreas.Contains(pa))
    {
        this.MyParkingAreas.Remove(pa);
    }
}


public override string ToString()
{
    return string.Format("{0}:{1},{2}", this.SSN, this.LastName, this.FirstName);
}


您将这样映射:

public class EmployeeMap : EntityTypeConfiguration<EmployeeEFEntity>
{
    public EmployeeMap()
    {
        // Primary Key
        this.HasKey(t => t.EmployeeUUID);

        this.Property(t => t.EmployeeUUID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        // Properties
        this.Property(t => t.TheVersionProperty)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();

        this.Property(t => t.SSN)
            .IsRequired()
            .HasMaxLength(11);

        this.Property(t => t.LastName)
            .IsRequired()
            .HasMaxLength(64);

        this.Property(t => t.FirstName)
            .IsRequired()
            .HasMaxLength(64);

        // Table & Column Mappings
        this.ToTable("Employee");
        this.Property(t => t.EmployeeUUID).HasColumnName("EmployeeUUID");
        this.Property(t => t.ParentDepartmentUUID).HasColumnName("ParentDepartmentUUID");
        this.Property(t => t.TheVersionProperty).HasColumnName("TheVersionProperty");
        this.Property(t => t.SSN).HasColumnName("SSN");
        this.Property(t => t.LastName).HasColumnName("LastName");
        this.Property(t => t.FirstName).HasColumnName("FirstName");
        this.Property(t => t.CreateDate).HasColumnName("CreateDate");
        this.Property(t => t.HireDate).HasColumnName("HireDate");

        // Relationships
        this.HasMany(t => t.MyParkingAreas)
            .WithMany(t => t.MyEmployees)
            .Map(m =>
            {
                m.ToTable("EmployeeParkingAreaLink");
                m.MapLeftKey("TheEmployeeUUID");
                m.MapRightKey("TheParkingAreaUUID");
            });

        this.HasRequired(t => t.ParentDepartment)
            .WithMany(t => t.Employees)
            .HasForeignKey(d => d.ParentDepartmentUUID);

    }
}


这样的ParkingArea:

[Serializable]
public partial class ParkingAreaEFEntity
{

    public ParkingAreaEFEntity()
    {
        CommonConstructor();
    }
    private void CommonConstructor()
    {
        //this.MyEmployees = new List<EmployeeEFEntity>();
    }

    public virtual Guid ParkingAreaUUID { get; set; }


    public virtual string ParkingAreaName { get; set; }
    public virtual DateTime CreateDate { get; set; }

    public virtual ICollection<EmployeeEFEntity> MyEmployees { get; set; }

    public virtual void AddEmployee(EmployeeEFEntity emp)
    {
        if (!emp.MyParkingAreas.Contains(this))
        {
            emp.MyParkingAreas.Add(this);
        }
        if (!this.MyEmployees.Contains(emp))
        {
            this.MyEmployees.Add(emp);
        }
    }

    public virtual void RemoveEmployee(EmployeeEFEntity emp)
    {
        if (emp.MyParkingAreas.Contains(this))
        {
            emp.MyParkingAreas.Remove(this);
        }
        if (this.MyEmployees.Contains(emp))
        {
            this.MyEmployees.Remove(emp);
        }
    }

关于c# - 通过连接表在 Entity Framework 中表达一对多关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37793197/

10-11 10:23