i want extend an entity framework model with a temporany attribute.
I need it only in a mvc form. I don't need save it in the db.
How can i do it?
Create a partial class for the entity you want to extend
e.g.
//must be in the same namespace as the Customer entity in the model
public partial class Customer
{
public string MyProperty{get;set;}
}
This property will be unmapped and you can fill it with data after you run a query or on materialization.
OR
Create a wrapper class for your entity which expose both the unmapped property and the mapped properties the properties you need in the view.
Related
I am using Model First to create my database with Entity Framework 6.
I have a class as follows:
public partial class User : IdentityUser
{
}
Where User is a class generated by EF in the designer. However, how can I map the in inherited properties of User to the columns of a table?
My biggest issue is that despite the fact that I am adding the inheritance of IdentityUser to the User class/table, when I go back into the EF designer, the properties of IdentityUser do not show up.
If you are using a non-abstract base class and do not specify a [Table] on your User, and therefore your User and IdentityUser map to a single table, then you are using the TPH (Table Per Hierarchy: Enable polymorphism by denormalizing the SQL schema, and utilize a type discriminator column that holds type information) scenario.
So, it seems like this is the one you need. You don't have to do anything special in Code First to enable TPH. It's the default inheritance mapping strategy. Just map it as any other entity and you're done!
Mapping example:
I am using EF 4.3 and am adding Audit fields to my classes and tables. I have a service layer which is getting the credentials of the client applications by using the OperationContext, so I am passing that information to my tables through EF mappings. An example of this would be:
class A
{
string CreatedByUser { get; set; }
}
class B : A
{
}
I am using the fluent interface to provide my POCO to table mappings -- when I map the CreatedByUser column in the base and derived class, the derived class mappings do not take effect and the information is not passed to the database.
I have gotten around this by creating fields in my base class for the derived classes to use that are just pass-throughs of the audit columns but this is messy.
Try making class A abstract, I think that will give you the effect you are looking for(add the columns from class A to all class/tables that inherit it)
I have added a calculated property to my Model (Entity Framework autogenerated class). How do I notify the ViewModel of any changes on this property? The calculated property sits in my partial class so the class is an EntityObject.
The EF EntityObject class has ReportPropertyChanged on every autogenerated property but how do I do it with my own calculated properties? If I try to use it, I get an ArgumentException: The property 'xxxxxx' does not have a valid entity mapping on the entity object.
From what I can understand, you have extended an EF entity to add a calculated property and you want to update the UI accordingly when your calculated property changes.
In that case, implement INotifyPropertyChanged on your partial class and then call OnPropertyChanged("propertyName") when your calculated property is updated.
I have exposed some EF5.0 entities generated from a database through at WCF data service.
The data service is consumed by a WPF-client which takes the entities (from the data service) and store them locally. I do this by creating a code-first entity database based on the WCF-entities:
public class LocalRaceContext : DbContext
{
public LocalRaceContext() { }
public LocalRaceContext(string connstr) : base(connstr) { }
public DbSet<Participant> Participants { get; set; }
.
.
. more ...
}
I want to extend the Participant with a new property (in the client-side model). I figured I could just do this with a partial class like so:
public partial class Participant
{
public virtual List<Stamp> Stamps { get; set; }
}
This however does not work. Do I need some kind of attribute on the partial class?
I get the following error:
"The type 'RaceEntities+Participant' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject."
Edit:
#IronMan84: The original model (without the partial class) works because EF code-first takes care of the database and table creation. Actually it works perfectly fine and I am able to save the EF model in a local SQL CE file and retrieve the objects again as EF-classes later.
What I'm trying to achieve is to persist data from the data service locally but in a model which is somewhat extended. I've succeeded so far up until the extending part.
#Matt Whetton: It fails when I create a new instance of LocalRaceContext.
Edit2: I've tried to make an empty partial class (no properties). It still throws the same error.
Thanks in advance
Frederik
Nested classes are not yet supported by EF. Move the Participant class outside of RaceEntities.
One of my entity classes would be possible to store in a sql server
database as a BIGINT. My question is: How do I get a Entity Framework
context to know how to store and retrieve instances of my entity class?
More detail. I'm using Noda Time, which can represent a (much) wider range of
dates than can SQL or .NET datetime (AND it's a dessert topping). My Entity Class, Happening, is a wrapper around NodaTime's
Instant class. I can set a Happening from a long, and get a long from
a happening with methods like .SetFromLong(long instant) and .ToLong().
Currently I have my model working, saving classes that contain
properties of the dot net DateTime type. If instead I want to use properties
of my custom type "Happening", how do I tell Entity Framework how to save those?
If I'm reading this article about Modeling and Mapping am I on the
right track or missing something simpler?
http://msdn.microsoft.com/en-us/library/bb896343.aspx
I'm using entity framework 4.
What i recommend doing is adding 2 properties on your entity a NodaTime and a long, and exclude your NodaTime property using [NotMapped] in your EF model, then in your getter/setter update the long.
ie
public class MyEntity{
public long TimeAsLong{get;set;}
[NotMapped]
public Happening {
get{
return new Happening().SetFromLong(TimeAsLong);
}
set {
TimeAsLong = value.ToLong();
}
}
}
The effect of this will be that the long is stored in the db but you can access it on the class via NodaTime