ASP.Net实体框架模型 [英] ASP.Net Entity Framework Model

查看:100
本文介绍了ASP.Net实体框架模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将属性添加到我的模型,不存在于数据库中?

Is it possible to add properties to my model that dont exist in the database?

例如我有一个日历表,我想在我的MVC控制器retireve这个数据然后制定出留待每个条目开始时间,这回视图。所以,我想在我的日历模式的另一个属性持有剩下时间,这是我将生成数据库以外的值。

For example I have a calendar table, I want to retireve this data in my MVC controller then work out time left until each entry starts and return this to the view. So I would like another property in my calendar model to hold time left which is a value that I will generate outside of the database.

我试着只是增加属性,但是当我这样做,我得到的错误,因为属性未映射到任何东西。

I've tried just adding the property but when I do that I get errors because the property is not mapped to anything.

这甚至可能吗?

感谢

推荐答案

您应该能够属性添加到模型中,但你将无法与LINQ查询它。 LINQ最终将建立和前pression它希望使用SQL对数据库运行。它在这一点上,你的LINQ将无法找到你的财产映射到字段的某个地方。

You should be able to add the property to the Model but you will not be able to query it with LINQ. LINQ will ultimately build and expression which it will want to run against the database using SQL. Its at that point that your LINQ will fail to find a mapping from your property to a field somewhere.

如果您的查询返回一个IEnumerable上已创建您的观点可能能够访问它的财产的实际类型。我不记得,如果EF在这种情况下,对测绘坚持认为,它可能会做的。

If your query returns an IEnumerable of the actual type on which you have created the property your view may be able to access it. I can't remember if EF insists on mapping in that case, it may do.

您可能会发现,如果你想提供一些其他的复合类型,以你的观点,你可以创建一个使用LINQ到对象随后LINQ查询。

You might find that you can create subsequent LINQ query that uses LINQ-to-objects if you want to provide some other composite type to your view.

这篇关于ASP.Net实体框架模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆