How do I define Foreign Key Optional Relationships in FluentAPI/Data Annotations with the Entity Framework?

前端 未结 4 2114
轻奢々
轻奢々 2020-12-31 00:16

I have a (sample) application with the following code:

public class Posts
{

    [Key]
    [Required]
    public int ID { get; set; }

    [Required]
    pub         


        
4条回答
  •  长情又很酷
    2020-12-31 00:55

    I would probably try to create the two one-to-one relationships as optional:required because a Poll must have a reference to Posts and a Post also must have a reference to Posts:

    modelBuilder.Entity()
        .HasOptional(x => x.Post)
        .WithRequired();
    
    modelBuilder.Entity()
        .HasOptional(x => x.Poll)
        .WithRequired();
    

    This makes Posts automatically the principal in the relationship and Post or Poll the dependent. The principal has the primary key in the relationship, the dependent the foreign key which is also the primary key at the same time in Post/Poll table because it is a one-to-one relationship. Only in a one-to-many relationship you would have a separate column for the foreign key. For a one-to-one relationship you also have to remove the foreign key columns PostId and PollId because Posts refers through its primary key to the Post and Poll.

    An alternative approach which seems to be appropriate in your model is inheritance mapping. Then the model would look like this:

    public abstract class BasePost  // your former Posts class
    {
        public int ID { get; set; }
        public string UserName { get; set; }
    }
    
    public class Post : BasePost
    {
        public string Text { get; set; }
        // other properties of the Post class
    }
    
    public class Poll : BasePost
    {
        // properties of the Poll class
    }
    

    You don't need the TypeOfPost then anymore because you can filter the two concrete types using the OfType LINQ operator, for example:

    var x = context.BasePosts.OfType()
        .Where(p => p.UserName == "Jim")
        .ToList();
    

    This would select all posts of a particular user but not the polls.

    You have to decide then which kind of inheritance mapping you want to use - TPH, TPT or TPC.

    Edit

    To get a one-to-many relationship you can specify the following mapping in Fluent API:

    modelBuilder.Entity()
        .HasOptional(x => x.Post)
        .WithMany()
        .HasForeignKey(x => x.PostID);
    
    modelBuilder.Entity()
        .HasOptional(x => x.Poll)
        .WithMany()
        .HasForeignKey(x => x.PollID);
    

    The foreign key properties must be nullable (int?) for this as you already found. Because the naming of your foreign key properties follows the naming convention EF uses for mapping you can omit the Fluent mapping altogether. It would only be required if you had unconventional names (like PostFK or something). You could then also use data annotations ([ForeignKey(...)] attribute) instead of Fluent API.

提交回复
热议问题