I have a entity
public class Avatar
{
[Key]
public int Id { get; set; }
[Required]
private string _linkInString { get; set; }
[NotMapped]
public Uri Link
{
get { return new Uri(_linkInString); }
set { _linkInString = value.AbsoluteUri; }
}
}
which has a private field (_linkInString)
because URI couldn't be saved in database as Uri, only as string.
When at first run, my database is creating I gettable Avatar with only Id column.
Any ideas on how to get around this?
开发者_运维知识库EDIT I just found out that: - names with underscore as first letter are abandoned. - use of internal gives the same result.
Create a public mapped string field which returns what you want to save as a string representation of your URI. Since your public URI is not mapped all works as you described of course.
Edit:
Please change this:
[Required]
private string _linkInString { get; set; }
Into this:
[Required]
public string _linkInString { get; set; }
How does it save now?
There is one very big limitation of code mapping in EF. It follows accessibility rules. So private field is private to your class and it is not mapped because context doesn't see it.
If you want to play with EF this way give up code first / fluent mapping and use EDMX where this is possible.
精彩评论