简体   繁体   English

如何在 C# 实体框架核心中使用 class 的构造函数,并将 class 作为字段?

[英]How do I use constructer for a class with a class as a field in C# Entity Framework Core?

I am just getting started with Entity Framework Core within C#, and I'm trying to set up a class structure where one class has a field that is another class.我刚刚开始使用 C# 中的实体框架核心,我正在尝试设置一个 class 结构,其中一个 class 有一个字段是另一个 class I have found that, when the classes do not have constructors, the code runs fine.我发现,当类没有构造函数时,代码运行良好。 However, when I introduce a constructor as follows:但是,当我按如下方式引入构造函数时:

    public class InterestEF
    {
        public InterestEF(string id, int numTimesInterestSelected, AdminEditEF lastEdit, ICollection<AdminEditEF> allEdits)
        {
            this.id = id;
            this.numTimesInterestSelected = numTimesInterestSelected;
            this.lastEdit = lastEdit;
            this.allEdits = allEdits;
        }

        [Key]
        public string id { get; set; }
        public int numTimesInterestSelected { get; set; }
        public AdminEditEF lastEdit { get; set; }
        public virtual ICollection<AdminEditEF> allEdits { get; set; }
    }

    public class AdminEditEF
    {
        public AdminEditEF(string id, string adminIdEditedBy, DateTime dateEdited, string changesMade, string reasonsForChanges, string idOfEditedEntity, EntityTypeEdited entityTypeEdited)
        {
            this.id = id;
            AdminIdEditedBy = adminIdEditedBy;
            this.dateEdited = dateEdited;
            this.changesMade = changesMade;
            this.reasonsForChanges = reasonsForChanges;
            this.idOfEditedEntity = idOfEditedEntity;
            this.entityTypeEdited = entityTypeEdited;
        }

        [Key]
        public string id { get; set; }
        public string AdminIdEditedBy { get; set; }
        public DateTime dateEdited { get; set; }
        public string changesMade { get; set; }
        public string reasonsForChanges { get; set; }
        public string idOfEditedEntity { get; set; }
        public EntityTypeEdited entityTypeEdited { get; set; }
    }

    public class MySQLEFContext : DbContext
    {
        public DbSet<AdminEditEF> AdminEdits { get; set; }

        public DbSet<InterestEF> interests { get; set; }

        public MySQLEFContext(DbContextOptions<MySQLEFContext> options): base(options) { }

    }

I get the following error:我收到以下错误:

System.InvalidOperationException: 'No suitable constructor found for entity type 'InterestEF'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'lastEdit' in 'InterestEF(string id, int numTimesInterestSelected, AdminEdit lastEdit)'.'

Basically, I'm just wondering if it's possible to have a class that has classes as fields that also has a set of constructors with parameters I can call elsewhere within code?基本上,我只是想知道是否有可能拥有一个具有类作为字段的 class,它还具有一组带有参数的构造函数,我可以在代码中的其他地方调用?

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you so much for reading!非常感谢您的阅读!

From the docs :文档

  • EF Core cannot set navigation properties (such as Blog or Posts above) using a constructor. EF Core 无法使用构造函数设置导航属性(例如上面的博客或帖子)。

So you would need a constructor that looks like this:因此,您需要一个如下所示的构造函数:

public InterestEF(string id, int numTimesInterestSelected)
{
    this.id = id;
    this.numTimesInterestSelected = numTimesInterestSelected;
}

Or a parameterless one:或无参数的:

public InterestEF()
{
}

暂无
暂无

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

相关问题 如何在 Entity Framework Core 中使用抽象 class? - How to use abstract class in Entity Framework Core? 如何在实体框架中使用类似字段的类 - How to use a class like a field in entity framework 如何使用自定义 class 作为具有 Entity Framework Core 转换的实体的属性? - How do you use a custom class as a property of an entity with a conversion with Entity Framework Core? 如何在 Entity Framework Core 中创建由基类和派生类组合而成的复合键 - How do I create composite key which is combination of base class and derived class in Entity Framework Core C#的子类中父类的构造方法 - parent class's constructer in a child class in C# 如何使用 Entity Framework Core 定义一个实体 class 作为一个集合在多个实体之间共享? - How do I define an entity class to be shared as a collection across multiple entities using Entity Framework Core? 如何通过“引用”分配给c#中的类字段? - How do I assign by “reference” to a class field in c#? 当类使用实体框架时,我需要在 C# 中创建一个析构函数吗? - Do I need to Create A Deconstructor in C# When The Class Uses Entity Framework? 如何在实体框架核心中将属性映射到类实例? - How can I map a property to a class instance in entity framework core? 如何在Entity Framework Core 2 / C#中实现简单的“复杂类型”? - How do I implement a simple “complex type” in Entity Framework Core 2/C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM