开发者

Entity Framework Code First Read Only Key

开发者 https://www.devze.com 2023-02-17 03:07 出处:网络
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?

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

精彩评论

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