开发者

Entity Framework Code First Read Only Key

In EF 4.1 RC1, I have a simple entity like say Category, with a property ID int. Can I make that a read only prope开发者_如何学Gorty and still have it work?

If not, how do you protect the PK/FK?


One way is to define your ID property like this:

public int ID { get; internal set; }

... then define your DbContext class in the same assembly as the "Category" class. It'll have write access to the property, but classes outside of the assembly won't.

If you need to define your DbContext in a separate assembly, you can use the InternalsVisibleTo attribute to let that assembly see the internals of your "Category" class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜