如何将非数据库属性添加到实体框架实体?

发布于 2024-11-16 07:27:03 字数 1546 浏览 10 评论 0原文

我的问题:我想向我的实体框架实体(在我的 ASP.NET 项目中)添加一个简单的类型属性(例如 string、int、double),该属性不与数据库中的任何字段关联实体对应的表。我希望能够在我的 ASP.NET 项目中设置此属性的值,并将属性的内容自动发送到 Silverlight 客户端,通过 RIA 服务获取实体。

我该怎么做?

注意:如果这样更容易,对于我的特定实例,我不需要将实体保存回数据库。另外,如果这适用于视图和表,那就太好了。

我的设置:我正在使用 Silverlight 4、Entity Framework 4 和 RIA 服务。它以正常方式设置:Silverlight 客户端应用程序和 ASP.NET 服务器应用程序;我正在从数据库生成 EF 模型。 RIA 服务在 Silverlight 客户端上生成实体和数据库访问方法。

我的示例:

数据库:Customer 表

  • CustomerID
  • CustomerName

EF 生成的实体(ASP.NET 服务器端): Customer 类

  • Public Property CustomerID as Integer
  • Public Property CustomerName as String

我想向与数据库不关联的客户实体:

  • Public Property UnicornColor as string

在我的域服务(ASP.NET 服务器端)中,我将自己填写新属性:

Public Function GetCustomers() As IQueryable(Of Customer)
    Dim customers as IQueryable(of Customer) = Me.ObjectContext.Customers
    For each c as Customer in customers
       c.UnicornColor = "Green"
    Next
    return customers
End Function

在客户端,我想要这个新属性当我跑步时它的价值就在那里我的查询:

Public Sub LoadCustomers()
    myContext.Load(myContext.GetCustomersQuery, AddressOf CustomersLoaded, Nothing)
End Sub

Public Sub CustomersLoaded(ByVal loadOp as LoadOperation(Of Customer))
    Dim customers as IEnumerable(Of Customer) = loadOp.Entities
    For Each c as Customer in customers
        dim color as string = c.UnicornColor
    Next
End Sub

My question: I would like to add a simple typed property (e.g. string, int, double) to my Entity Framework entity (in my ASP.NET project) that is not associated with any field in the database table that corresponds to the entity. I would like to be able to set the value of this property in my ASP.NET project and have the contents of the property auto-magically sent over to the Silverlight client gets the entities via RIA Services.

How do I do this?

Note: If this makes it easier, for my particular instance, I don't need to save the entities back to the database. Also, it would be great if this worked for views as well as tables.

My setup: I am using Silverlight 4, Entity Framework 4, and RIA Services. It is set up in the normal fashion: Silverlight client application and an ASP.NET server application; I am generating my EF model from the database. RIA services is generating the entities and database access methods on the Silverlight client.

My example:

Database: Customer table

  • CustomerID
  • CustomerName

EF generated entity (ASP.NET server-side): Customer class

  • Public Property CustomerID as Integer
  • Public Property CustomerName as String

I'd like to add a property to the Customer entity that is not associated with the database:

  • Public Property UnicornColor as string

In the my domain service (ASP.NET server side), I'll fill in the new property myself:

Public Function GetCustomers() As IQueryable(Of Customer)
    Dim customers as IQueryable(of Customer) = Me.ObjectContext.Customers
    For each c as Customer in customers
       c.UnicornColor = "Green"
    Next
    return customers
End Function

On the client side, I'd like this new property and its values to be there when I run my query:

Public Sub LoadCustomers()
    myContext.Load(myContext.GetCustomersQuery, AddressOf CustomersLoaded, Nothing)
End Sub

Public Sub CustomersLoaded(ByVal loadOp as LoadOperation(Of Customer))
    Dim customers as IEnumerable(Of Customer) = loadOp.Entities
    For Each c as Customer in customers
        dim color as string = c.UnicornColor
    Next
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2024-11-23 07:27:03

使用 T4s 生成实体的分部类(例如 Visual Studio 扩展库中的 POCO T4s),然后添加一个文件,例如 MyEntity.Part2.cs,其分部类与生成的文件相同,但包含新属性。

有关更多信息,请谷歌部分 C# 类。

Use T4s to generate partial classes for entities (e.g. the POCOs T4s in the Visual Studio extension gallery) then add a file e.g. MyEntity.Part2.cs with the same partial class as the generated file, but that contains the new properties.

For more info Google partial classes C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文