为什么我会收到"该修改“虚拟”是无效的这个项目"错误? [英] Why am I getting "The modifier 'virtual' is not valid for this item" error?
本文介绍了为什么我会收到"该修改“虚拟”是无效的这个项目"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个MVC应用程序如下模型:(的code是大,我认为这将是您更容易理解。)
公共类职位
{
公众诠释的JobId {搞定;组; }
公共字符串名称{;组; } 公开名单<人才与GT; GetJobs()
{
清单<人才与GT; jobsList =新的List<人才与GT;();
jobsList.Add(新工作{的JobId = 1,名称=操作员});
jobsList.Add(新工作{的JobId = 2,名称=表演者});
jobsList.Add(新工作{的JobId = 3,名称=头});
返回jobsList;
}
}公共类Person
{
公共虚拟INT PERSONID {搞定;组; }
公共字符串全名{获得;组; }
公众诠释的JobId {搞定;组; }
公共虚拟招聘工作;
公共字符串电话{搞定;组; }
公共字符串地址{搞定;组; }
公共字符串护照{搞定;组; }
[数据类型(DataType.MultilineText)
公共字符串评论{搞定;组; }
}公共类PersonPaidTo:人
{
[键]
公共覆盖INT PERSONID {搞定;组; }
公共虚拟目录<排序>订单{搞定;组; }
}公共类头:人
{
[键]
公共覆盖INT PERSONID {搞定;组; }
公开招聘工作{搞定;组; }
公共头()
{
Job.Id = 3;
}
}
我在课堂人
错误在该领域工作
:
修改器虚拟是无效的此项目
块引用>解决方案是的,这code是无效的:
public虚拟招聘工作;
这是声明的字段的,而字段不能是虚拟的。要么你希望它是一个属性:
public虚拟招聘职位{搞定;组; }
或只是一个领域:
//伊克,公共领域!
公开招聘工作;(我的猜测是,你想要前者,但两者都是有效的C#)。
I'm trying to create mvc application with model below: (the code is large. I think it will be more understandable for you)
public class Job { public int JobId { get; set; } public string Name { get; set; } public List<Job> GetJobs() { List<Job> jobsList = new List<Job>(); jobsList.Add(new Job { JobId = 1, Name = "Operator" }); jobsList.Add(new Job { JobId = 2, Name = "Performer" }); jobsList.Add(new Job { JobId = 3, Name = "Head" }); return jobsList; } } public class Person { public virtual int PersonId { get; set; } public string FullName { get; set; } public int JobId { get; set; } public virtual Job Job; public string Phone { get; set; } public string Address { get; set; } public string Passport { get; set; } [DataType(DataType.MultilineText)] public string Comments { get; set; } } public class PersonPaidTo : Person { [Key] public override int PersonId { get; set; } public virtual List<Order> Orders { get; set; } } public class Head : Person { [Key] public override int PersonId { get; set; } public Job Job { get; set; } public Head() { Job.Id = 3; } }
I have an error in class
Person
in the fieldJob
:The modifier 'virtual' is not valid for this item
解决方案Yes, this code is invalid:
public virtual Job Job;
That's declaring a field, and fields can't be virtual. You either want it to be a property:
public virtual Job Job { get; set; }
Or just a field:
// Ick, public field! public Job Job;
(My guess is that you want the former, but both are valid C#.)
这篇关于为什么我会收到&QUOT;该修改“虚拟”是无效的这个项目&QUOT;错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文