本文介绍了如何向实体框架实体添加非数据库属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我该怎么做?



注意:如果这样更容易,对于我特定的实例,不需要将实体保存回数据库。另外,如果这对于视图和表格起作用,那将是很棒的。



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



我的示例:



数据库:客户表




  • CustomerID

  • CustomerName



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




  • 公共财产CustomerID为整数

  • 公共财产CustomerName as String



喜欢添加一个不与数据库关联的客户实体的属性:




  • 公共属性UnicornColor作为字符串



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



pre> 公共函数GetCustomers()作为IQueryable(Of Customer)
将客户定义为IQueryable(Customer)= Me.ObjectContext.Customers
对于每个c作为客户在客户
c.Unicorn Color =Green
Next
返回客户
结束功能

在客户端,当我运行我的查询时,我希望这个新的属性及其值在那里:

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

Public Sub CustomersLoaded(ByVal loadOp as LoadOperation(Of Customer))
将客户视为IEnumerable(Of Customer)= loadOp.Entities
对于每个c作为客户的客户
dim color as string = c.UnicornColor
下一个
End Sub


解决方案

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



有关更多信息,Google部分类C#。


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
解决方案

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#.

这篇关于如何向实体框架实体添加非数据库属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:32