Entity Framework Code First : how to annotate a foreign key for a “Default” value?

前端 未结 4 1025
[愿得一人]
[愿得一人] 2020-12-02 20:32

I have 2 classes: Client and Survey.

Each Client can have many surveys - but only one default survey.

I have defined the classes like this:

p         


        
相关标签:
4条回答
  • 2020-12-02 20:54

    The problem is that when you have multiple relationships between two entities, EF Code First isn't able to find out which navigation properties match up, unless, you tell it how, here is the code:

    public class Client
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
    
        public string ClientName { get; set; }
    
        /****Change Nullable<int> by int?, looks better****/
        public int? DefaultSurveyID { get; set; }
    
        /****You need to add this attribute****/
        [InverseProperty("ID")]
        [ForeignKey("DefaultSurveyID")]
        public virtual Survey DefaultSurvey { get; set; }
    
        public virtual ICollection<Survey> Surveys { get; set; }
    }
    

    With your previous version, EF was creating that extra relationship because it didn't know that the DefaultSurvey property was referencing the ID of the Survey class, but you can let it know that, adding the attribute InverseProperty whose parameter is the name of the property in Survey you need DefaultSurvey to match with.

    0 讨论(0)
  • 2020-12-02 21:00

    Entity Framework does exactly what it's told to do. What you've told it is that there is both a one-to-many and a one-to-one relationship between Clients and Surveys. It generated both FKs in the Survey table in order to map both of the relationships that you've requested. It has no idea that you're trying to connect the two relationships together, nor do I think does it have the ability to deal with that.

    As an alternative you might want to consider adding a IsDefaultSurvey field on the Survey object so that you can query for the default survey through the Surveys collection that you have on the Client object. You could even go one step further and put it in as a NotMapped property on the Client object so that you could still use Client.DefaultSurvey to get the correct survey, and not have to change any of your other code, as follows:

    [NotMapped]
    public Survey DefaultSurvey
    {
      get { return this.Surveys.First(s => s.IsDefaultSurvey); }
    }
    
    0 讨论(0)
  • 2020-12-02 21:03

    You can do it using code-first, but not being a code first expert I cheated :-)

    1) I created the tables and relationships (as above without the extra Client_ID) in the database using SMS

    2) I used Reverse Engineer Code First to create the required classes and mappings

    3) I dropped the database and recreated it using context.Database.Create()

    Original table defs:

    CREATE TABLE [dbo].[Client](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Name] [nvarchar](50) NULL,
        [DefaultSurveyId] [int] NULL,
         CONSTRAINT [PK_dbo.Client] PRIMARY KEY NONCLUSTERED 
        (
            [Id] ASC
        )
    )
    
    CREATE TABLE [dbo].[Survey](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Name] [nvarchar](50) NULL,
        [ClientId] [int] NULL,
         CONSTRAINT [PK_dbo.Survey] PRIMARY KEY NONCLUSTERED 
        (
            [Id] ASC
        )
    )
    

    Plus foreign keys

    ALTER TABLE [dbo].[Survey]  WITH CHECK 
        ADD CONSTRAINT [FK_dbo.Survey_dbo.Client_ClientId] FOREIGN KEY([ClientId])
        REFERENCES [dbo].[Client] ([Id])
    
    ALTER TABLE [dbo].[Client]  WITH CHECK 
        ADD CONSTRAINT [FK_dbo.Client_dbo.Survey_DefaultSurveyId] 
        FOREIGN KEY([DefaultSurveyId]) REFERENCES [dbo].[Survey] ([Id])
    

    Code generated by reverse engineering:

    public partial class Client
    {
        public Client()
        {
            this.Surveys = new List<Survey>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
        public int? DefaultSurveyId { get; set; }
        public virtual Survey DefaultSurvey { get; set; }
        public virtual ICollection<Survey> Surveys { get; set; }
    }
    
    public partial class Survey
    {
        public Survey()
        {
            this.Clients = new List<Client>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
        public int? ClientId { get; set; }
        public virtual ICollection<Client> Clients { get; set; }
        public virtual Client Client { get; set; }
    }
    
    public class ClientMap : EntityTypeConfiguration<Client>
    {
        #region Constructors and Destructors
    
        public ClientMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);
    
            // Properties
            this.Property(t => t.Name).HasMaxLength(50);
    
            // Table & Column Mappings
            this.ToTable("Client");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.DefaultSurveyId).HasColumnName("DefaultSurveyId");
    
            // Relationships
            this.HasOptional(t => t.DefaultSurvey)
                .WithMany(t => t.Clients).HasForeignKey(d => d.DefaultSurveyId);
        }
    
        #endregion
    }
    
    public class SurveyMap : EntityTypeConfiguration<Survey>
    {
        #region Constructors and Destructors
    
        public SurveyMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);
    
            // Properties
            this.Property(t => t.Name).HasMaxLength(50);
    
            // Table & Column Mappings
            this.ToTable("Survey");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.ClientId).HasColumnName("ClientId");
    
            // Relationships
            this.HasOptional(t => t.Client)
                .WithMany(t => t.Surveys).HasForeignKey(d => d.ClientId);
        }
    
        #endregion
    }
    
    0 讨论(0)
  • 2020-12-02 21:06

    Please notice that adding the code below you will fix the issue.

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext() : base("DefaultConnection")
        {
    
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
              modelBuilder.Entity<Client>()
                          .HasOptional(x => x.DefaultSurvey)
                          .WithMany(x => x.Surveys);
                          .HasForeignKey(p => p.DefaultSurveyID);
        {
    }
    
    0 讨论(0)
提交回复
热议问题