views:

281

answers:

2

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

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.

Is this even possible?

Thanks

A: 

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.

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.

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.

AnthonyWJones
A: 

It's a non-persistent property or transient. I don't know Entity Framwork well but with a quick google search you should find the answer.

BTW you can find a lot of tips here : http://weblogs.asp.net/zeeshanhirani/archive/2008/12/18/my-christmas-present-to-the-entity-framework-community.aspx

After making a quick search myself and a test in VS2008 I don't see a way to exclude a property from the mapping. Maybe it requires you to edit manually the mapping file ? :(

Matthieu