[英]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!
非常感谢您的阅读!
- 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.