我用的是实体框架是第一次,想知道如果下面是可能的 - 我已经产生从DB我的课,并有一个叫类别
I am using the Entity Framework for the first time and want to know if the following is possible - I have generated my classes from the DB, and have one called Category.
显然,这有我的表中的所有字段(ID,类别名称,SortOrder的等),但我想知道如果我可以添加自定义属性这是不是在桌子上,但实际上是一个自定义方法的结果
Obviously it has all my fields in the table (ID, CategoryName, SortOrder etc..) but I want to know if I can add a custom property which is not in the table, but is actually the result of a custom method.
我要添加一个名为CategoryURL新的物业基本上是通过自定义的方法类别名称属性运行,并返回一个字符串联用
I want to add a new property called 'CategoryURL' which is basically the 'CategoryName' property run through a custom method and returns a hyphenated string.
我最初的想法是从生成的Category类继承和创造这样的事情里面呢?
My initial thought is inheriting from the generated Category class and creating something like this inside?
public string CategoryURL { get{ return MyCustomMethod(this.CategoryName) } }这是正确的做法?而将this.CategoryName的工作,因为我觉得它应该?基本上,最终的结果是,当我返回类别我想这是这个类,所以我可以在我的foreach循环使用它的一部分的列表。
Is this the correct approach? And will 'this.CategoryName' work as I think it should? Basically the end result is when I return a list of 'Category' I want this to be part of the class so I can use it in my foreach loop.
希望这是有道理的?
推荐答案您应该使用 部分 类:
you should use a partial class:
public partial class Category { public string CategoryURL { get{ return MyCustomMethod(this.CategoryName); } } }本办法 this.CategoryName 将只是正常工作。
This way this.CategoryName will work just as expected.
这工作,因为由实体框架code生成器生成的类也产生部分类。这也意味着,你可以放心地重新生成从数据库中的类,而不会影响你自己定义的部分类。
This works because the classes generated by the entity framework code generator also generates partial classes. It also means that you can safely re-generate the classes from the database without affecting the partial classes that you have defined yourself.
更多推荐
添加自定义属性的实体框架?
发布评论