I am using EF 4.1 and was look for a nice workaround for the lack of enum support. A backing property of int seems logical.
[Required]
public VenueType Type
{
get { return (VenueType) TypeId; }
set { TypeId = (int) value; }
}
private int TypeId { get; set; }
But how can I make this property private and still map it. In other words:
How can I map a private property using EF 4.1 code first?
Another workaround might be to set your field as internal:
and add it to tour model:
Another way to handle this is to defines a custom entity configuration and add a binding for that.
In your class add a class that inherits from EntityTypeConfiguration (This can be found in System.Data.Entity.ModelConfiguration)
In your context add the following:
Wish I could say I found this on my own, but I stumbled upon it here:https://romiller.com/2012/10/01/mapping-to-private-properties-with-code-first/
As seen in your model you grant read access to the property. So maybe you want to block set access and map to EF using a private setter. Like this.
Here's a convention you can use in EF 6+ to map selected non-public properties (just add the
[Column]
attribute to a property).In your case, you'd change TypeId to:
In your
DbContext.OnModelCreating
, you'll need to register the convention:Finally, here's the convention:
Extending @crimbo's answer above ( https://stackoverflow.com/a/21686896/3264286 ), here's my change to include public properties with private getters:
you can't map private properties in EF code first. You can try it changing it in to
protected
and configuring it in a class inherited fromEntityConfiguration
.Edit
Now it is changed , See this https://stackoverflow.com/a/13810766/861716