实体框架关系不起作用

问题描述 投票:0回答:1

我正在使用实体框架将这些对象存储在数据库中。

主要类是

Event
类。非列表的
Event
属性可以正常工作并且保存得很好。但是当从数据库
Event
获取特定的
MeetupDateVoteOptions
时,
Comments
始终为空。我不知道为什么。

using System.ComponentModel.DataAnnotations;

namespace Family_Meetup.Models
{
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Comment
    {
        public Comment() { }

        public Comment(string username, string comment)
        {
            this.username = username;
            this.comment = comment;
        }

        [Key]
        public Guid Id { get; set; }

        public string username { get; private set;  }
        public string comment { get; private set;  }
    }

    public class MeetupDateVoteOption
    {
        public MeetupDateVoteOption() { }

        public MeetupDateVoteOption(DateTime date, List<string> votedusers, Guid id)
        {
            this.Id = Id;
            this.date = date;
            this.votedusers = votedusers;
        }

        [Key]
        public Guid Id { get; set; }

        public DateTime date { get; private set;  }
        public List<string> votedusers { get; private set;  }
    }

    public class Event
    {
        public Event() { }

        public Event(Guid id, string title, string creator, string description, string location, DateTime creationdate, int maxvotesondate, int maxvotesbyuser, List<Comment> comments, List<MeetupDateVoteOption> meetupdatevoteoptions, List<string> userWhiteList)
        {
            this.id = id;
            this.title = title;
            this.creator = creator;
            this.description = description;
            this.location = location;
            this.creationdate = creationdate;
            this.maxvotesondate = maxvotesondate;
            this.maxvotesbyuser = maxvotesbyuser;
            this.comments = comments;
            this.meetupdatevoteoptions = meetupdatevoteoptions;
            this.userWhiteList = userWhiteList;
        }

        public const int maxTitleLength = 200;

        [Key]
        public Guid id { get; private set;  }
        public string title { get; private set;  }
        public string creator { get; private set;  }
        public string description { get; private set;  }
        public string location { get; private set;  }
        public DateTime creationdate { get; private set;  }
        public int maxvotesondate { get; private set;  }
        public int maxvotesbyuser { get; private set;  }

        public List<string> userWhiteList { get; private set; }
        public List<Comment> comments { get; private set;  }
        public List<MeetupDateVoteOption> meetupdatevoteoptions { get; private set;  }
    } 
}

上下文如下:

using Family_Meetup.Models;
using Family_Meetup.Persistance.Configurations;
using Microsoft.EntityFrameworkCore;

namespace Family_Meetup.Persistance
{
    public class FamilyMeetupDbContext : DbContext 
    {
        public FamilyMeetupDbContext(DbContextOptions<FamilyMeetupDbContext> options) : base(options)
        {
        }

        public DbSet<Event> Events { get; set; } = null!;

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfiguration(new FamilyMeetupConfiguration());
            modelBuilder.ApplyConfiguration(new MeetupDateVoteOptionConfiguration());
        }
    }
}

我期望它会自己找到表之间的关系。

database entity-framework database-relations
1个回答
0
投票

将集合导航初始化为空列表,而不是 null。

例如

public ICollection<Comment> Comments { get; } = new List<Comment>();

看到

https://learn.microsoft.com/en-us/ef/core/modeling/relationships/navigations#collection-navigations

© www.soinside.com 2019 - 2024. All rights reserved.