简体   繁体   English

在实体框架中需要多个外键之一

[英]Require One of Multiple Foreign Keys in Entity Framework

I'm basically trying to enforce this in Entity Framework: Require Only One Of Multiple Columns Be Not Null我基本上是想在实体框架中强制执行这一点: 只要求多列之一不是 Null

My database has several 1:m relationships where the child entity belongs to one of several parent entities.我的数据库有几个 1:m 关系,其中子实体属于几个父实体之一。 For example, let's say I have tables for Teachers , Students , and Guardians .例如,假设我有TeachersStudentsGuardians的表格。 Each of those can have many PhoneNumbers and EmailAddresses .每个都可以有很多PhoneNumbersEmailAddresses I am using EF Code First, and my models look something like:我使用的是 EF Code First,我的模型看起来像:

public class Teacher {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<PhoneNumber> PhoneNumbers { get; set; }
    public List<EmailAddress> EmailAddresses { get; set; }
}

public class Student {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<PhoneNumber> PhoneNumbers { get; set; }
    public List<EmailAddress> EmailAddresses { get; set; }
}

public class Guardian {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<PhoneNumber> PhoneNumbers { get; set; }
    public List<EmailAddress> EmailAddresses { get; set; }
}

public class PhoneNumber {
    public int Id { get; set; }
    public string Number { get; set; }
}

public class EmailAddress {
    public int Id { get; set; }
    public string Email { get; set; }
}

When I run the migration, this creates the database with the tables/columns I would expect.当我运行迁移时,这会创建包含我期望的表/列的数据库。 The PhoneNumbers and EmailAddresses tables each have columns Teacher_Id , Student_Id , and Guardian_Id , which are foreign keys to their respective parent entity. PhoneNumbersEmailAddresses表各有列Teacher_IdStudent_IdGuardian_Id ,它们是各自父实体的外键。 However, there are no constraints on how many parent entities can be set on the child.但是,对子实体可以设置多少父实体没有限制。 For example, I can create a PhoneNumber that has all three parent IDs set to null, or I can set both a Teacher_Id and a Guardian_Id .例如,我可以创建一个PhoneNumber ,将所有三个父 ID 都设置为 null,或者我可以同时设置Teacher_IdGuardian_Id

I tried adding a required attribute to the parents like so:我尝试像这样向父母添加必需的属性:

public class Teacher { // Also Student/Guardian
    public int Id { get; set; }
    public string Name { get; set; }

    [Required]
    public List<PhoneNumber> PhoneNumbers { get; set; }

    [Required]
    public List<EmailAddress> EmailAddresses { get; set; }
}

That does not seem to have any effect.这似乎没有任何效果。

I think there is no way to do this on entities.我认为没有办法对实体执行此操作。 Instead, create a migration and try to alter the table in migration class like this:相反,创建一个迁移并尝试更改迁移 class 中的表,如下所示:

    public partial class YourMigrationName: Migration
                            {
                              protected override void Up(MigrationBuilder migrationBuilder)
                                {
                                    migrationBuilder.Sql("ALTER TABLE [dbo].[PhoneNumber]  
    WITH CHECK ADD CONSTRAINT [CK_PhoneNumer_Teacher_Student_Guardian] CHECK  (Teacher_Id
 is not null or Student_Id is not null or Guardian_Id is not null)
                            GO
                            ALTER TABLE [dbo].[EmailAddress]  WITH CHECK ADD  CONSTRAINT 
            [CK_EmailAddress_Teacher_Student_Guardian] CHECK  (Teacher_Id is not null or 
        Student_Id is not null or Guardian_Id is not null)
                        ");
                                }
                            }

Try this:尝试这个:

public class PhoneNumber 
{
    public int Id { get; set; }
    public string Number { get; set; }
    //Added this code:
    [Required] 
    public Teacher Teacher { get; set;}
}

public class EmailAddress {
    public int Id { get; set; }
    public string Email { get; set; }
    //Added this code:  
    [Required]
    public Teacher Teacher { get; set;}
}

That would make sure you cannot create PhoneNumber without Teacher.这将确保您无法在没有教师的情况下创建电话号码。 You can also do this:你也可以这样做:

public class PhoneNumber 
{
    public int Id { get; set; }
    public string Number { get; set; }
    public int TeacherId
    public Teacher Teacher { get; set;}
}

That would also detect that you want to add constraint on your PhoneNumber.这也将检测到您想在您的电话号码上添加约束。 Both ways work fine.两种方式都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM