如何启用映射实体的私有财产 [英] How to enable mapping the private property of the entity

查看:175
本文介绍了如何启用映射实体的私有财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始时我不得不说对不起我的英语水平。

At the begining I have to say sorry for my english.

我使用的EntityFramework和code-第一种方式。我有我的头一个想法,但我不知道如何使它真正。我不是在编程初学者,但我总是用流利的NHibernate。

I'm using EntityFramework and code-first way. I have one idea in my head but I dont know how to make it real. I'm not the begginer in programming, but I was always using fluent NHibernate.

我有一个实体:

public class User
{
    [Key]
    public Int32 Id { get; set; }
    public String Name { get; set; }
    // This property should be mapped on DB
    private String Surname { get; set; }

    // This one shouldn't be mapped
    public String GetSurname { get { return Surname; } set { Surname = "SomePrefix." + value; }   }
}

问题1.我该如何映射实体的私有财产吧?

Question 1. How can I map the private property of the entity?

我无法访问OnModelCreating()我在上下文中ovverride这个私有财产。

I can't access this private property in my ovverride of OnModelCreating() in context.

2,问我怎样才能禁用一个公共财产的映射?

Question 2. How can I disable the mapping of the one public property?

作为结果,我想: 当我尝试设置姓我添加了开始时的preFIX。

As the result I want to get: When I try to set the Surname i add the prefix in the begining.

推荐答案

实体框架没有提供的映射私有财产的方式,因为它需要的读/写值给他们,所以它必须至少有一个公共的get / set为属性。

Entity framework does not provide a way of mapping private properties as it needs to read/write values to them, so it must have at least a public get/set for properties.

在回答你的,你可以使用模型构建器的第二个问题(我的preferred描述你的映射如下方式)

In answer to your second question you can either use the modelbuilder (my preferred way of describing your mappings as below)

modelBuilder.Entity<User>().Ignore(u=>u.GetSurname);

或添加属性

[NotMapped]
private String Surname { get; set; }

这篇关于如何启用映射实体的私有财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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