为什么导航属性必须要加virtualbox 添加硬盘

EF导航属性延迟加载为null
这些天一直在弄一个项目demo,底层是用EF来实现的。因为也是初学,多数靠网上找来的demo照猫画虎。昨天碰到一个问题,导航属性一直取不到值。导航属性加载有三种方式,默认应该是延迟加载的。
要使用延时加载需要有两个设置,第一,导航属性必须使用virtual修饰;第二,DBContext类构造函数中this.Configuration.LazyLoadingEnabled的值必须为true。我两项都满足,但是导航属性还是为null.
经过排查,终于找出罪魁祸首,就是this.Configuration.ProxyCreationEnabled =
false 这个属性设置导致的。在将该属性注释后,延时加载终于可以正常工作了。
& & public class
TestRCDbContext : DbContext
& public TestRCDbContext()
base("name=TestRCDbConn")
//this.Configuration.ProxyCreationEnabled =
&this.Configuration.LazyLoadingEnabled =
//此行也可以注释掉,因为默认设置就是true
& public virtual DbSet Schools { }
& public virtual DbSet Classes { }
& public virtual DbSet Teachers {
& public virtual DbSet Students {
& public virtual DbSet Dictionaries {
& protected override void
OnModelCreating(DbModelBuilder modelBuilder)
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Virtual PC使用教程:Virtual PC安装使用完全指南一个绝好的软件,他在你的电脑上能同时模拟多台电脑,虚拟的电脑使用起来与一台真实的电脑一样,可以进行bios设定,可以给它的硬盘进行分区,格式化,操作系统你可以安装dos,windows 95, windows 98,windowa me,windows 2000,windows xp,Windows Server 2003 ,等等,你可以在你的win98里跑winxp,是不是很有意思呢? 安装:请从多特软件站下载(不需要原版),安装过程没有特殊要求,一路点击 NEXT 按钮就能顺利的安装好。从开始菜单点击 Connectix Virtual PC 运行软件。第一次运行 Virtual PC 时的界面: 在你第一次启动Virtual PC 时会弹出“PC 安装向导”,他的意思是相当于“你已经从电脑市场购买了组装一台电脑所需的所有部件(当然在你的电脑上这些部件都是虚拟的,Virtual PC 就是虚拟的电脑的意思。),“PC 安装向导”就是帮你把这些设备组装起来并配置好。首先给我们的电脑起一个名字,假设我们要安装win98操作系统,因此此台虚拟电脑就叫win98。
那么点击下一步,来到配置选项界面,配置选项将引导你配置你的虚拟硬盘,内存的大小,你想安装的操作系统。创建默认PC选项是当你非常熟悉此软件的使用方法后来进行配置用的。由于是首次使用 Virtual PC,因此我们使用设置向导,点击下一步。
我们想安装的操作系统是win98,那就选win98,你也可以点击下拉菜单看看在 Virtual PC 都能安装哪些操作系统。嘿!只要你的硬盘足够大,你可以把各种操作系统都安装一下来看看他们到底是什么模样。点击下一步。
现在来配置虚拟电脑的内存(并不是 Virtual PC 运行时自身占用的内存),它占用的是你的电脑上的物理内存,也就是说如果你的电脑使用的内存容量为256M,而在此配置的内存容量为80M(如下图所示), 当启动起虚拟的电脑时它要占用掉物理内存中的80M来运行要安装的win98,这时你现在正在运行的操作系统就只剩下176M可用内存,因此你想让你的虚拟电脑能很好的运行,你的物理内存必须足够大。安装dos、 win3.1 、win95、win98等这些需内存小的操作系统,物理内存必须在128M以上,像安装win2000、winxp、linux等操作系统,物理内存最好在256M以上。
经过前几步,我们已经知道虚拟的电脑要安装的操作系统是win98,它的内存有80M大小,那它的硬盘在哪儿呢?好!接下来的步骤就是创造我们的硬盘。首次使用 Virtual PC 当然没有硬盘了,因此选“创建一个新的硬盘映像文件”选项,点击下一步。
这一步是选择所建虚拟硬盘所在的物理硬盘上的分区,虚拟硬盘事实上就是一个单独的文件,只有 Virtual PC 才能读取,而且还是动态的,就是此文件的大小由你装入其中的内容多少来决定。如果你装的操作系统是dos,那么此文件只有几十M大小,如果装的操作系统是winxp,那么此文件可能就有1.5G大小。如图二所示,我们的硬盘位置是:H:win98.vhd图一:
来到这一步,配置步骤完毕,结果见下图。咦!硬盘竟有16384MB,硬盘上根本放不下这么大的一个文件。不要紧,前面我说过,此文件(虚拟硬盘)是动态的,这个数字只表明此文件最大是16384MB大小。点击完成按钮来结束 PC 安装向导。
正如你所见,本来空白的 Virtual PC 列表框中多出了win98 “已关闭”一项,右边的“属性设置...”按钮、“删除..”按钮、“启动系统”按钮也变为可用状态。下面两幅图是配置前后的比较。配置前: 配置后: 首先让我们来看看面板上的这几个按钮是来做什么的。“新建PC...”按钮,喔!原来是刚才所作的操作。那么就依照前面的操作来建一台Linux,结果如下图(配置Linux过程中有的过程会有小小的变化,自己考虑一下为什么会这样)。 “属性设置..”按钮是用来做什么的呢?原来是用来调整前面完成的配置内容的,在此你可以更改该PC的名称,改变内存的大小,更换所使用的硬盘(硬盘映像文件),你要使用的串并行端口,还有共享文件夹等。每一项都有说明,很容易理解的,但也要仔细揣摩一番。其实使用此软件的大部分过程都有使用说明,当遇到问题时仔细看一下说明问题也许就会迎刃而解。
再来看一下“删除...”按钮是来做什么的。点击删除按钮弹出下面对话框:如图: 点击OK按钮后的结果是在Virtual PC 列表中没有了Linux,正如上图所说明的,Linux的硬盘映像文件并没有删除,可以在硬盘上找到。
现在来让我们点击“启动系统”按钮来看看会发生什么事,原来出现了一个新窗口,下面的画面好熟悉呀,像是电脑的BIOS界面,你看到了吗?我是怎么打开此窗口的呢?答案是出现此新窗口时按下键盘上的“Delete”键,事实上从点击“启动系统”按钮开始,所有的操作就像在一台真的电脑上操作那样,包括各种操作:关机、重启、硬盘分区、格式化硬盘、从软驱启动、从可启动光盘启动、安装操作系统。是的,我们并没有给win98这台PC安装操作系统,因此出现了“OS not found”、“Install an OS on this hard drive”的提示,意思是你没有安装操作系统。接下来让我们在这台虚拟机上安装win98操作系统。
首先把一张可光盘启动的 win98 操作系统安装盘放入你的光驱(在我的机器上LG-DVD光驱的盘符是I:,你的机器上可能是其他盘符),点击“CD”选项,弹出菜单,“载入主驱动器I:”变为可用状态,也就是说,当你的光驱里放入光盘后,Virtual PC 才能使用你的光驱。载入主驱动器I:,其操作过程如图: 另外,如果在你的电脑上没有光驱,你可以直接载入ISO文件来使用。对于Virtual PC来说,ISO文件载入后就相当于光驱。你可以用ISO编辑软件把win98或其它操作系统做成可启动ISO文件,然后加载此文件来进行操作系统的安装。下面就是加载ISO文件的过程。 点击 PC(P)-&重启(R)...启动到 dos 我选的是第2项,既启动到dos下并加载光驱,因为我们要从光盘上安装操作系统吗。我怎么没有进行分区和格式化硬盘呢?因为在默认情况下Virtual PC所创建的虚拟硬盘都是FAT32格式,而我只想装一个操作系统作为演示,所以没有进行更详细的操作,如你对分区和格式化硬盘感兴趣可以自己试一下。回车,进行下一步,到了下面的画面。
提示光驱所在盘符为D:,因此转到D盘并输入setup;来进行安装win98,按回车开始安装。 用鼠标点击继续按钮,咦!鼠标怎么被锁定在 Virtual PC 的窗口中了,不要紧,按下键盘上的右侧Alt键,鼠标又被释放了出来。当你对此软件非常熟悉以后就会了解到,键盘上的右侧Alt键就是Virtual PC默认的“热键”,举例:“热键+Enter” 用来在窗口和全屏模式下进行转换,你自己可以实际操作一下。
安装过程中需注意的是在有的安装环节会有很长的时间没有反应,请耐心的等一下。还有当安装程序重启时请先把光驱释放出来,要不然会启动到dos下(因为光盘是可启动光盘)。操作如下图,点击“释放主驱动器I:”,或干脆把光盘弹出光驱。 进行n步后终于把win98安装好了。接下来是安装/更新附加模块。附加模块是修正已安装好的操作系统在使用上的不方便和增强操作系统的功能的程序。安装 Virtual PC 时,已附带一模块,但此模块只适用于windows操作系统,安装此模块后,操作系统能以32位真彩800*600分辨率下显示,并且鼠标不再限制在窗口之内,而且可以共享物理硬盘上的文件夹,在虚拟PC中通过共享文件夹来使用你的物理硬盘上的数据。安装过程如图:点击“安装/更新附加模块(I)”。
稍等一会儿,出现安装程序,如下图,一路 NEXT 安装完成即可。
接下来是共享文件夹的使用方法。把鼠标指向如下图文件夹那个位置,单击右键,弹出“共享设置...”、“共享文件夹..”菜单,点击“共享文件夹”。 转到此画面,我设定共享的是D:(程序)盘,盘符为“Z”,设定为总是共享(下次启动时还共享相同的文件夹)。
既下图:看到了吗?那个“程序(Z:)”,你可以对它进行读写操作。
下面简单的讲讲网络的使用。如果你想在客户机中上网,首先在属性设置--&网络设置中设置为“共享网络模式”。接下来在你的客户机中安装“Internet 连接共享”组件(以win98为例)。
完成共享连接向导,重启后就能上网了。当然,你的真实的电脑必须已经进行了拨号连接,见下图右下角。 我也只能说这么多啦,其它操作自己摸索一下吧!我想不会把聪明的你难倒的。
欢迎转载:
相关推荐:为什么Nhibernate中属性和方法必须Virtual的_百度知道
为什么Nhibernate中属性和方法必须Virtual的
我有更好的答案
那您一定碰到过下面的错误:NHibernate.OrderLine,为什么它一开始的时候必须要设置成virtual的,这是一个对于初次接触NHibernate的人经常有的疑惑,我们知道任何真正的ORM必须要有的一个重要功能就是延迟加载,ORM会负责加载当前需要的数据.InvalidProxyTypeException如果你曾经用过NHibernate 2.0或者更高的版本: NHibernateExamples: The following types may noprotected virtual‘ or ‘protected internal virtual‘哎呀,我们忘记把OrderLine实体中的UnitPrice属性标志成virtual的了,奇怪的是。这应该是ORM的职责。理想情况下应该是.Entities.OrderLine: method get_UnitPrice should be ‘public/protected virtual‘ or ‘protected internal virtual‘ NHibernateExamples.Entities,你能访问这些属性,而且如果这时数据还没有加载,当你第一次访问的时候。针对这个问题,最简洁的答案就是,如果你通过ORM获取一个对象,你不会希望它去自动的获取整个对象图中所有数据(默认情况下应当不是), 还有你也不希望添加一些凌乱的代码去检查特定的关联关系是否已经被加载了,我们需要的是只有我们的需要的时候才去加载它们:因为我们需要把成员设置成virtual的,是为了实现我们的延迟加载的魔幻功能。但是更详细点的答案反而更有趣: method set_UnitPrice should be ‘public&#47
为您推荐:
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。First the explanation and the gist, then the question. So:
Let's say I have a view AccountView defined in EF (6.1.1) database first (edmx) so that the code-generated class is
//This class is generated from a view by EF (edmx)...
public partial class AccountView
public System.Guid Id { }
public int CompanyId { }
Then I create a partial class in the same namespace (Entities) as
[MetadataType(typeof(AccounViewMetaData))]
public partial class AccounView
//This is added here explicitly. AccountView itself exposes just
//a naked key, CompanyId.
public virtual Company Company { }
//This is just in case...
public class AccounViewDomainMetaData
//This is to add a navigation property to the OData $metadata. How to do this
//in WebApiConfig? See as follows...
[ForeignKey("Company")]
public int CompanyId { }
//This is an EF generated class one from an edmx..-
public partial class Company
public Company()
public int CompanyID { }
public string Name { }
And then in WebApiConfig I Write, or try to, something in OData v4 (the newest, 6.9.0.0 series with WebApi, the newest also) as follows
builder.EntitySet&Entities.Company&("Companies");
var accountSet = builder.EntitySet&Entities.AccountView&("Accounts");
accountSet.EntityType.HasKey(i =& i.Id); //EF has hard time recognizing primary keys on database first views...
//TODO: How should I define the required binding so that I could "?$expand=Company"
//as such as ``http://example.com/service/Accounts?$expand=Company``
//accountSet.HasRequiredBinding(i =& i.CompanyId, typeof(Entities.Company));
The resulting $metadata is like
&EntityType Name="Company"&
&PropertyRef Name="CompanyID"/&
&Property Name="CompanyID" Type="Edm.Int32" Nullable="false"/&
&Property Name="Name" Type="Edm.String"/&
&/EntityType&
&EntityType Name="AccountView"&
&PropertyRef Name="Id"/&
&Property Name="Id" Type="Edm.Guid" Nullable="false"/&
&Property Name="CompanyId" Type="Edm.Int32" Nullable="false"/&
&NavigationProperty Name="Company" Type="Entities.Company" Nullable="false"/&
&/EntityType&
Question(s): As mentioned in the TODO comment, how should I define
A navigational property so that I could call http://example.com/Accounts?$expand=Company or http://example.com/Accounts?$Companies?
Now it works if I call
http://example.com/Accounts or
http://example.com/Accounts(someId)
Then if I do something like
http://example.com/Accounts?$expand=Companies
http://example.com/Accounts?$expand=Company or
http://example.com/Accounts(someId)?$expand=Company
I get greeted by HTTP 400 (?$expand=Companies) or HTTP 500 (?$expand=Company).
I succeeded with creating a Containment relationship already. It looks like, though, it requires having the root entity defined by an ID, whereas I'd like to provide "GET" to the root and optionally expand to a "child list" objects (hence this question about ?$expand=).
Now the case is having one non-optional expand done for one entity, but I suspect the next thing I'd like have is a scenario where there's a list of entities (or companies in terms of this specific example) but how could I achieve these scenarios? How to fix even this case of expanding to the first always existing sub-object?
My controller is defined as follows
public class AccountsController: ODataController
[EnableQuery]
public IHttpActionResult Get()
//This context here is a EF entities model (generated from an edmx)...
return Ok(Context.AccountView);
catch(Exception ex)
return InternalServerError();
[EnableQuery]
public IHttpActionResult Get([FromODataUri]Guid key)
return Ok(SingleResult.Create(Context.AccountView.Where(a =& a.Id == key)));
catch (Exception ex)
return InternalServerError(ex);
What I'm basically trying to do is to add some more metadata to database first, edmx, views and mimick the article . Thus far with no success...
&edit 1: The plot thickens, so to speak. The HTTP 500 error comes from an internal exception (I had to turn on breaking to managed framework exceptions) that says
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
Additional information: The specified type member 'Company' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
So, yes, AccountView is a view and it does not have a direct foreign key relation to Company table (which just happens to be a table, but it could be a view too). How could I go to add one? I though adding metadata did the trick already, as evidenced by $metadata information. Doesn't OData just write an INNER JOIN in the database? Am I missing something here? ... This seem to be related to the way I added the Company reference and EF doesn't like that. It looks I should go about adding it to the OData model only...
&edit 2: It doesn't seem to make a difference (at least not with the setting I have) if I change the AccountView CompanyId to CompanyID to match the casing that of the definition in Company table.
&edit3: I asked another, related question to this, .
解决方案 It looks I got this one answered as to related another post and little pushing from another forum.
About the exception, indeed, it was caused by the query hitting to the database and trying to retrieve columns which aren't defined there. For more information:
Then as for the code and answer, the other question is . The code a bit augmented here is as follows
public async Task&IHttpActionResult& Get(ODataQueryOptions&T& options)
IQueryable&T& tempQuery = Context.AccountV
IQueryable&T result = tempQ
if(options.SelectExpand != null)
if(options.Filter != null)
tempQuery = options.Filter.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable&T&;
/* Other options that should go to the DB level... */
//The results need to be materialized, or otherwise EF throws a
//NotSupportedException due to columns and properties that aren't in the DB...
result = (await tempQuery.ToListAsync()).AsQueryable();
//Do here the queries that can't be hit straight by the queries. E.g.
//navigation properties not defined in EF views (that can't be augmented)...
//E.g. get the company here.
//This is needed to that OData formatter knows to render navigation links too.
Request.ODataProperties().SelectExpandClause = options.SelectExpand.SelectExpandC
return Ok(result);
本文地址: &
首先,解释和要点,那么问题。所以:比方说,我有一个观点 AccountView 在EF定义(6.1.1)数据库第一(EDMX),以便code生成的类是 由EF(EDMX)从视图生成//这个类...公共部分类AccountView{
公众的System.Guid标识{搞定;组; }
公众诠释CompanyId {搞定;组; }} 然后,我创建了相同的命名空间部分类(实体)的
[MetadataType(typeof运算(AccounViewMetaData))]公共部分类AccounView{
//这是在这里明确地添加。 AccountView本身只是暴露
//赤裸裸的关键,CompanyId。
公共虚拟公司公司{搞定;组; }
//这是以防万一......
公共类AccounViewDomainMetaData
//这是一个导航属性添加到OData的$元数据。如何做到这一点
//在WebApiConfig?见如下...
[ForeignKey的(“本公司”)]
公众诠释CompanyId {搞定;组; }
//这是一个的EF生成的类从EDMX ..-公共部分类公司{
上市公司()
公众诠释CompanyID {搞定;组; }
公共字符串名称{;组; }} 然后在 WebApiConfig 我写,或尝试,在OData的V4的东西(最新,6.9.0.0系列的WebAPI,最新的也有)如下:
builder.EntitySet< Entities.Company>(“公司”);VAR accountSet = builder.EntitySet< Entities.AccountView>(“帐户”);accountSet.EntityType.HasKey(ⅰ= GT; i.Id); //英孚已经很难识别数据库第一观点的主键...// TODO:我应该如何界定,这样我可以要求结合“$ =拓展公司?”//为如``HTTP://example.com/service/Accounts $扩大= Company``//accountSet.HasRequiredBinding(i => i.CompanyId的typeof(Entities.Company)); 生成的 $元像 <&架构GT;
<的EntityType名称=“公司”>
<关键>
< PropertyRef NAME =“CompanyID”/>
< / Key与GT;
<属性名=“CompanyID”TYPE =“Edm.Int32”可空=“假”/>
<属性名称=“名称”TYPE =“Edm.String”/>
< /&的EntityType GT;
<的EntityType名称=“AccountView”>
<关键>
< PropertyRef名称=“ID”/>
< / Key与GT;
<属性名=“ID”TYPE =“Edm.Guid”可空=“假”/>
<属性名=“CompanyId”TYPE =“Edm.Int32”可空=“假”/>
< NavigationProperty名称=“公司”类型=“Entities.Company”可空=“假”/>
< /&的EntityType GT;< /模式>
问题(S):正如 TODO 评论中提及,我应该如何定义的导航属性,这样我可以打电话给 http://example.com/Accounts?$expand=Company 或的http://例子.COM /账户?$公司?现在它的工作原理,如果我称之为
http://example.com/Accounts 或
http://example.com/Accounts(someId) 然后,如果我做这样的事情。
http://example.com/Accounts?$expand=Companies
http://example.com/Accounts?$expand=Company 或
http://example.com/Accounts(someId)?$ =拓展公司 我得到映入眼帘的 HTTP 400 (?$展开;公司)或 HTTP 500 (?$ =拓展公司)。我会成功,但创建一个遏制关系了。它看起来像,不过,它需要具有由一个ID定义的根实体,而我想提供“GET”为根和可选扩展到“子榜”对象(因此这个问题呢?$ =扩展) 现在的情况是有一个非可选的展开做了一个实体,但我怀疑,我想未来的事情已经是那里的列表的场景实体(或公司在这个具体的例子而言),但我怎么能实现这些场景?如何修复即使这种情况下的扩展应用于第一总是存在的子对象?我的控制器被定义如下 公共类AccountsController:ODataController{
[EnableQuery]
公共IHttpActionResult获得()
//这里这方面是一个EF实体模型(从EDMX生成)...
返回OK(Context.AccountView);
赶上(异常前)
返回InternalServerError();
[EnableQuery]
公共IHttpActionResult获取([FromODataUri] GUID项)
返回OK(SingleResult.Create(Context.AccountView.Where(一个= GT; a.Id ==键)));
赶上(异常前)
返回InternalServerError(除息);
}} 什么我基本上试图做的是添加一些更多的元数据数据库第一,EDMX,观点和模拟天生的文章使用$选择,$扩大,并在的ASP.NET Web API的OData 2 $ 的价值。迄今没有成功...
<编辑1:越来越复杂了,可以这么说。 HTTP 500错误来自内部异常(我不得不把打破托管框架例外),上面写着
“System.NotSupportedException”类型的第一次机会异常出现在EntityFramework.dll
其他信息:指定的类型成员“公司”不支持LINQ到实体。只有初始化,实体成员和实体导航属性都支持。所以,是的, AccountView 是一个视图,它没有直接的外键关系公司表(这恰好是一个表,但也可以是一个视图太)。我怎么可能去增加一个?我虽然添加元数据的伎俩已经由 $元信息作为证明。不只是OData的写 INNER JOIN 在数据库中?我失去了一些东西在这里? ...这似乎与我添加了公司引用和EF不喜欢这样的方式。它看起来我应该去了解它添加到OData的模型仅仅...
<编辑2:这似乎并没有发挥作用(至少不符合我的设置),如果我更改 AccountView
CompanyId 到 CompanyID 相匹配的外壳,在公司定义的表。 < EDIT3:我问另外一个相关的问题这一点,How在从EF模型与ODataConventionModelBuilder建立了一个模型添加复杂的属性。解决方案 这看起来我这人回答为相关的另一篇文章和小从另一个论坛上推。关于异常,事实上,它是由该查询击中到数据库并尝试检索它们没有定义的列引起的。欲了解更多信息:
然后作为code和答案,另一个问题是,Expanding导航性能与ODataQueryOptions 。在code有点这里增加如下: 公共异步任务< IHttpActionResult>获取(ODataQueryOptions< T&选项){
IQueryable的< T> tempQuery = Context.AccountV
IQueryable的<吨结果= tempQ
如果(options.SelectExpand!= NULL)
如果(options.Filter!= NULL)
tempQuery = options.Filter.ApplyTo(tempQuery,新ODataQuerySettings())为IQueryable的< T&GT ;;
/ *这应该去DB级别其他选项... * /
//结果需要被物化,否则EF抛出
//引发NotSupportedException由于列和不在DB属性...
结果=(等待tempQuery.ToListAsync())AsQueryable已();
//在这里做不能直接通过查询击中查询。例如。
//在EF意见没有定义导航属性(即不能增加)...
//例如。在这里得到了公司。
//这是需要的OData的格式化知道要呈现导航链接了。
Request.ODataProperties()SelectExpandClause = options.SelectExpand.SelectExpandClause。}返回OK(结果);
本文地址: &
扫一扫关注官方微信}

我要回帖

更多关于 virtualbox 硬件加速 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信