首先映射私有属性实体框架代码
我正在使用 EF 4.1,并正在寻找一个很好的解决方法来解决缺乏枚举支持的问题。 int 的支持属性似乎是合乎逻辑的。
[Required]
public VenueType Type
{
get { return (VenueType) TypeId; }
set { TypeId = (int) value; }
}
private int TypeId { get; set; }
但我怎样才能将这个属性设为私有并仍然映射它。换句话说:
如何首先使用 EF 4.1 代码映射私有属性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以在 EF 6+ 中使用以下约定来映射选定的非公共属性(只需将
[Column]
属性添加到属性)。在您的情况下,您可以将 TypeId 更改为:
在您的
DbContext.OnModelCreating
中,您需要注册约定:最后,这是约定:
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:
您无法首先在 EF 代码中映射私有属性。您可以尝试将其更改为
protected
并在继承自EntityConfiguration
的类中配置它。编辑
现在改了,看这个https://stackoverflow.com/a/13810766/861716
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
另一个解决方法可能是将您的字段设置为内部:
并将其添加到游览模型中:
Another workaround might be to set your field as internal:
and add it to tour model:
如果您喜欢
Attributes
(像我一样)并且您正在使用 EF Core。您可以使用以下方法。首先,创建一个属性来标记私有或属性:
然后,标记您想要保持私有的属性。
最后,在
DbContext
中,将其放在OnModelCreating
方法的末尾:If you like
Attributes
(like me) and you're using EF Core. You could use the following approach.First, Create an attribute to mark private or properties:
Then, mark the properties you would like to stay private.
Finally, in your
DbContext
, place this at the end of yourOnModelCreating
method:试试这个。
然后在DbContext中
Try this.
and then in DbContext
扩展@crimbo上面的答案( https://stackoverflow.com/a/21686896/3264286 ),这是我要包括的更改具有私有 getter 的公共属性:
Extending @crimbo's answer above ( https://stackoverflow.com/a/21686896/3264286 ), here's my change to include public properties with private getters:
处理此问题的另一种方法是定义自定义实体配置并为其添加绑定。
在您的类中添加一个继承自 EntityTypeConfiguration 的类(这可以在 System.Data.Entity.ModelConfiguration 中找到)
在您的上下文中添加以下内容:
希望我可以说我自己找到了这个,但我在这里偶然发现了它:< a href="https://romiller.com/2012/10/01/mapping-to-private-properties-with-code-first/" rel="nofollow noreferrer">https://romiller.com/2012/10/01/mapping-to-private-properties-with-code-first/
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/
正如您的模型中所见,您授予对该属性的读取访问权限。因此,也许您想阻止集合访问并使用私有 setter 映射到 EF。像这样。
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.